From b868be69c9c8a72c6b83d5ba20eda4c65eec5c9c Mon Sep 17 00:00:00 2001 From: Brian Flowers Date: Sat, 5 Nov 2016 12:08:12 -0400 Subject: [PATCH] Adding error checking to DB connections --- configure.php | 10 +++++++++- graph.php | 33 ++++++++++++++++++++++++--------- index.php | 21 ++++++++++++++++----- 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/configure.php b/configure.php index 3d45c40..65970f5 100755 --- a/configure.php +++ b/configure.php @@ -2,7 +2,15 @@ // /home/bdsm must be bdsm:www-root 775 // /home/bdsm/bdsm.sqlite3* must be bdsm:www-root 775 -$db = new PDO("sqlite:/home/bdsm/bdsm.sqlite3"); +try +{ + $db = new PDO("sqlite:/home/bdsm/etc/bdsm/bdsm.db"); +} +catch(PDOException $e) +{ + echo 'Connection failed: ' . $e->getMessage(); +} + $result = $db->query("SELECT * FROM bdsmweb"); if( $db->errorInfo()[0] == "HY000" ) diff --git a/graph.php b/graph.php index 12802b9..e5ff497 100644 --- a/graph.php +++ b/graph.php @@ -1,18 +1,28 @@ query("SELECT DISTINCT host FROM MEASUREMENTS"); +} +catch(PDOException $e) +{ + echo 'Connection failed: ' . $e->getMessage(); +} + +if( !$result ) +{ + echo "Error: No data found in database file /home/bdsm/etc/bdsm/bdsm.db"; + echo "Query: SELECT DISTINCT host FROM MEASUREMENTS"; +} + $colors = Array("#55F", "#5F5", "#F55", "#5FF", "#F5F", "#FF5", "#555"); -$result = $db->query("SELECT DISTINCT host FROM MEASUREMENTS;"); $hosts = Array(); while($host = $result->fetch()) if ( $host['host'] ) array_push($hosts, $host['host']); $host = urldecode($_GET['host']); -//print_r($hosts); -//$result = $db->query("SELECT * FROM bdsmweb;"); -//print_r($result->fetch()); - if( !in_array($host, $hosts) ) die("Error: Host not found."); ?> @@ -48,13 +58,18 @@ if( !in_array($host, $hosts) ) context.lineWidth = 2; query("SELECT attribute, name FROM bdsmweb WHERE host='$host' AND graph=1;"); + if( !$result ) + { + echo "Error: No measurements found in database file /home/bdsm/etc/bdsm/bdsm.db for host $host"; + echo "Query: SELECT attribute, name FROM bdsmweb WHERE host='$host' AND graph=1;"; + } + $attributes = Array(); - while($attribute = $result->fetch()) -{ + { if ( $attribute['attribute'] ) $attributes[$attribute['attribute']] = $attribute['name']; -} + } $i = 0; foreach (array_keys($attributes) as $attribute) { diff --git a/index.php b/index.php index e32a40f..c6d2ea2 100755 --- a/index.php +++ b/index.php @@ -13,20 +13,31 @@
query("SELECT DISTINCT host FROM MEASUREMENTS"); +} +catch(PDOException $e) +{ + echo 'Connection failed: ' . $e->getMessage(); +} -$db_handle = new SQLite3("/home/bdsm/bdsm.sqlite3"); -$result = $db_handle->query("SELECT DISTINCT host FROM MEASUREMENTS"); +if( !$result ) +{ + echo "Error: No data found in database file /home/bdsm/etc/bdsm/bdsm.db"; + echo "Query: SELECT DISTINCT host FROM MEASUREMENTS"; +} $hosts = Array(); -while($host = $result->fetchArray(SQLITE3_ASSOC)) +while($host = $result->fetch()) if ( $host['host'] ) array_push($hosts, $host['host']); foreach ($hosts as $host) { $host = urlencode($host); - echo ""; } ?> -- 1.8.3.1