diff --git includes/database.inc includes/database.inc
index 0a68008..9612d17 100644
--- includes/database.inc
+++ includes/database.inc
@@ -392,7 +392,7 @@ function db_rewrite_sql($query, $primary_table = 'n', $primary_field = 'nid',  $
  * Adds the DISTINCT flag to the supplied query if a DISTINCT doesn't already
  * exist in the query. Returns the altered query.
  *
- * This will not, and never did guarantee that you will obtain distinct 
+ * This will not, and never did guarantee that you will obtain distinct
  * values of $table.$field.
  *
  * @param $table Unused. Kept to remain API compatibility.
@@ -419,6 +419,25 @@ function db_escape_table($string) {
 }
 
 /**
+ * Determine whether the previous query caused an error.
+ */
+function db_error() {
+  return db_set_last_error(FALSE);
+}
+
+/**
+ * @param $errno
+ *   Remembers the last error code if not FALSE, returns the last error code otherwise.
+ * @return
+ *   Error code.
+ */
+function db_set_last_error($errno = FALSE) {
+  static $last_errno;
+  if ($errno !== FALSE) $last_errno = $errno;
+  else return $last_errno;
+}
+
+/**
  * @} End of "defgroup database".
  */
 
diff --git includes/database.mysql.inc includes/database.mysql.inc
index 45c03cf..acfcbb8 100644
--- includes/database.mysql.inc
+++ includes/database.mysql.inc
@@ -119,13 +119,17 @@ function _db_query($query, $debug = 0) {
     print '<p>query: '. $query .'<br />error:'. mysql_error($active_db) .'</p>';
   }
 
-  if (!mysql_errno($active_db)) {
+  $errno = mysql_errno($active_db);
+  db_set_last_error($errno);
+  if (!$errno) {
     return $result;
   }
   else {
     // Indicate to drupal_error_handler that this is a database error.
     ${DB_ERROR} = TRUE;
     trigger_error(check_plain(mysql_error($active_db) ."\nquery: ". $query), E_USER_WARNING);
+    // restore last error after watchdog resetted it
+    db_set_last_error($errno);
     return FALSE;
   }
 }
@@ -183,14 +187,6 @@ function db_result($result) {
 }
 
 /**
- * Determine whether the previous query caused an error.
- */
-function db_error() {
-  global $active_db;
-  return mysql_errno($active_db);
-}
-
-/**
  * Determine the number of rows changed by the preceding query.
  */
 function db_affected_rows() {
diff --git includes/database.mysqli.inc includes/database.mysqli.inc
index 1571fb6..4c46a54 100644
--- includes/database.mysqli.inc
+++ includes/database.mysqli.inc
@@ -119,13 +119,17 @@ function _db_query($query, $debug = 0) {
     print '<p>query: '. $query .'<br />error:'. mysqli_error($active_db) .'</p>';
   }
 
-  if (!mysqli_errno($active_db)) {
+  $errno = mysqli_errno($active_db);
+  db_set_last_error($errno);
+  if (!$errno) {
     return $result;
   }
   else {
     // Indicate to drupal_error_handler that this is a database error.
     ${DB_ERROR} = TRUE;
     trigger_error(check_plain(mysqli_error($active_db) ."\nquery: ". $query), E_USER_WARNING);
+    // restore last error after watchdog resetted it
+    db_set_last_error($errno);
     return FALSE;
   }
 }
@@ -185,14 +189,6 @@ function db_result($result) {
 }
 
 /**
- * Determine whether the previous query caused an error.
- */
-function db_error() {
-  global $active_db;
-  return mysqli_errno($active_db);
-}
-
-/**
  * Determine the number of rows changed by the preceding query.
  */
 function db_affected_rows() {
diff --git includes/database.pgsql.inc includes/database.pgsql.inc
index fef5c53..c701908 100644
--- includes/database.pgsql.inc
+++ includes/database.pgsql.inc
@@ -151,6 +151,8 @@ function _db_query($query, $debug = 0) {
     print '<p>query: '. $query .'<br />error:'. pg_last_error($active_db) .'</p>';
   }
 
+  $error = ($last_result === FALSE) ? 0 : pg_last_error($active_db);
+  db_set_last_error($error);
   if ($last_result !== FALSE) {
     return $last_result;
   }
@@ -158,6 +160,8 @@ function _db_query($query, $debug = 0) {
     // Indicate to drupal_error_handler that this is a database error.
     ${DB_ERROR} = TRUE;
     trigger_error(check_plain(pg_last_error($active_db) ."\nquery: ". $query), E_USER_WARNING);
+    // restore last error after watchdog resetted it
+    db_set_last_error($error);
     return FALSE;
   }
 }
@@ -213,14 +217,6 @@ function db_result($result) {
 }
 
 /**
- * Determine whether the previous query caused an error.
- */
-function db_error() {
-  global $active_db;
-  return pg_last_error($active_db);
-}
-
-/**
  * Returns the last insert id. This function is thread safe.
  *
  * @param $table
