Adding error checking to DB connections
authorBrian Flowers <git-admn@bsflowers.net>
Sat, 5 Nov 2016 16:08:12 +0000 (12:08 -0400)
committerBrian Flowers <git-admn@bsflowers.net>
Sat, 5 Nov 2016 16:08:12 +0000 (12:08 -0400)
configure.php
graph.php
index.php

index 3d45c40..65970f5 100755 (executable)
@@ -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" )
index 12802b9..e5ff497 100644 (file)
--- a/graph.php
+++ b/graph.php
@@ -1,18 +1,28 @@
 <?php
-$db  = new PDO("sqlite:/home/bdsm/bdsm.sqlite3");
+try
+{
+  $db = new PDO("sqlite:/home/bdsm/etc/bdsm/bdsm.db");
+  $result = $db->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;
 <?php
   $result = $db->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)
     {
index e32a40f..c6d2ea2 100755 (executable)
--- a/index.php
+++ b/index.php
     <div id="charts">
 
 <?php
-$colors = Array("#55F", "#5F5", "#F55", "#5FF", "#F5F", "#FF5", "#555");
+try
+{
+  $db  = new PDO("sqlite:/home/bdsm/etc/bdsm/bdsm.db");
+  $result     = $db->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 "<iframe src='graph.php?host=$host' class='graph' scrolling='no' />";
+    echo "<iframe src='graph.php?host=$host' class='graph' scrolling='no'></iframe>";
 }
 ?>