Index: /trunk/lib/PDO.inc.php
===================================================================
--- /trunk/lib/PDO.inc.php	(revision 697)
+++ /trunk/lib/PDO.inc.php	(revision 698)
@@ -192,17 +192,23 @@
             $this->dbh = new \PDO($dsn, $this->getParam('db_user'), $this->getParam('db_pass'), $options);
         } catch (\PDOException $e) {
-            $mysql_error_msg = sprintf('PDOException: (%s) %s', $e->getCode(), $e->getMessage());
-            // sprintf('PDO error: Could not connect to server (db_server=%s, db_name=%s, db_user=%s, db_pass=%s)', $this->getParam('db_server'), $this->getParam('db_name'), $this->getParam('db_user'), ('' == $this->getParam('db_pass') ? 'NO' : 'YES'));
+            $mysql_error_msg = sprintf('PDO connect %s: %s (db_server=%s, db_name=%s, db_user=%s, db_pass=%s)',
+                get_class($e),
+                $e->getMessage(),
+                $this->getParam('db_server'),
+                $this->getParam('db_name'),
+                $this->getParam('db_user'),
+                ('' == $this->getParam('db_pass') ? 'NO' : 'YES')
+            );
             $app->logMsg($mysql_error_msg, LOG_EMERG, __FILE__, __LINE__);
 
             // Print helpful or pretty error?
-            if ($this->getParam('db_debug')) {
-                echo $mysql_error_msg . "\n";
+            if ($this->getParam('db_debug') && $app->getParam('display_errors')) {
+                if (!$app->isCLI()) {
+                    printf('<pre style="padding:1em;background:#ddd;font:0.9rem monospace;">%s</pre>', $mysql_error_msg);
+                }
             }
 
             // Die if db_die_on_failure = true, or just continue without connection.
             return $this->_fail();
-
-            // throw new \PDOException($e->getMessage(), (int)$e->getCode());
         }
 
@@ -233,5 +239,8 @@
     public function close()
     {
+        $app =& App::getInstance();
+
         if (!$this->_connected) {
+            $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__);
             return false;
         }
@@ -268,6 +277,9 @@
     public function ping()
     {
+        $app =& App::getInstance();
+
         if (!$this->_connected) {
-           return false;
+            $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__);
+            return false;
         }
 
@@ -362,5 +374,6 @@
 
         if (!$this->_connected) {
-           return false;
+            $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__);
+            return false;
         }
 
@@ -372,5 +385,5 @@
                 dump($debugqry, true, SC_DUMP_PRINT_R, __FILE__, __LINE__);
             } else {
-                echo "<!-- ----------------- Query $this->_query_count ---------------------\n$debugqry\n-->\n";
+                echo "<!-- ----------------- PDO query $this->_query_count ---------------------\n$debugqry\n-->\n";
             }
         }
@@ -387,10 +400,12 @@
         try {
             $stmt = $this->dbh->query($query);
-        } catch (\PDOException $e) {
-            $mysql_error_msg = sprintf('PDOException: (%s) %s', $e->getCode(), $e->getMessage());
-            $app->logMsg(sprintf('PDOException (%s): %s in query: %s', $e->getCode(), $e->getMessage(), $debugqry), LOG_EMERG, __FILE__, __LINE__);
-            if ($this->getParam('db_debug')) {
+            if (!$stmt) {
+                throw new Exception('PDO::query returned false');
+            }
+        } catch (\Exception $e) {
+            $app->logMsg(sprintf('PDO query %s (%s): %s in query: %s', get_class($e), $e->getCode(), $e->getMessage(), $debugqry), LOG_EMERG, __FILE__, __LINE__);
+            if ($this->getParam('db_debug') && $app->getParam('display_errors')) {
                 if (!$app->isCLI()) {
-                    printf('<pre style="padding:2em; background:#ddd; font:9px monospace;">%s<hr>%s</pre>', wordwrap($e->getMessage()), htmlspecialchars($debugqry));
+                    printf('<pre style="padding:1em;background:#ddd;font:0.9rem monospace;">%s<hr>%s</pre>', wordwrap($e->getMessage()), htmlspecialchars($debugqry));
                 }
             }
@@ -416,5 +431,6 @@
 
         if (!$this->_connected) {
-           return false;
+            $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__);
+            return false;
         }
 
@@ -423,5 +439,5 @@
         $debugqry = preg_replace("/\n[\t ]+/u", "\n", $query);
         if ($this->getParam('db_always_debug')) {
-            echo "<!-- ----------------- Prepare $this->_query_count ---------------------\n$debugqry\n-->\n";
+            echo "<!-- ----------------- PDO prepare $this->_query_count ---------------------\n$debugqry\n-->\n";
         }
 
@@ -437,10 +453,12 @@
         try {
             $stmt = $this->dbh->prepare($query, ...$params);
+            if (!$stmt) {
+                throw new Exception('PDO::query returned false');
+            }
         } catch (\PDOException $e) {
-            $mysql_error_msg = sprintf('PDOException: (%s) %s', $e->getCode(), $e->getMessage());
-            $app->logMsg(sprintf('PDOException (%s): %s in prepare: %s', $e->getCode(), $e->getMessage(), $debugqry), LOG_EMERG, __FILE__, __LINE__);
-            if ($this->getParam('db_debug')) {
+            $app->logMsg(sprintf('PDO prepare %s (%s): %s in query: %s', get_class($e), $e->getCode(), $e->getMessage(), $debugqry), LOG_EMERG, __FILE__, __LINE__);
+            if ($this->getParam('db_debug') && $app->getParam('display_errors')) {
                 if (!$app->isCLI()) {
-                    printf('<pre style="padding:2em; background:#ddd; font:9px monospace;">%s<hr>%s</pre>', wordwrap($e->getMessage()), htmlspecialchars($debugqry));
+                    printf('<pre style="padding:1em;background:#ddd;font:0.9rem monospace;">%s<hr>%s</pre>', wordwrap($e->getMessage()), htmlspecialchars($debugqry));
                 }
             }
@@ -509,4 +527,5 @@
 
         if (!$this->_connected) {
+            $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__);
             return false;
         }
