From 3342593eac2440bbf70fa06a484b273d8e5e075e Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Wed, 7 Dec 2005 15:17:27 +0000
Subject: [PATCH] - Patch #38889 by rkerr: db_error and db_affected_rows did
 not work properly in presence of multiple database connections.

---
 includes/database.mysql.inc | 20 ++++++++++++--------
 includes/database.pgsql.inc |  7 ++++---
 2 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/includes/database.mysql.inc b/includes/database.mysql.inc
index 04af5ea0ac9a..c41e00494bbe 100644
--- a/includes/database.mysql.inc
+++ b/includes/database.mysql.inc
@@ -63,7 +63,7 @@ function db_connect($url) {
     drupal_maintenance_theme();
     drupal_set_title('Unable to select database');
     print theme('maintenance_page', '<p>We were able to connect to the MySQL database server (which means your username and password is okay) but not able to select the database.</p>
-<p>The MySQL error was: '. theme('placeholder', mysql_error()) .'.</p>
+<p>The MySQL error was: '. theme('placeholder', mysql_error($connection)) .'.</p>
 <p>Currently, the database is '. theme('placeholder', substr($url['path'], 1)) .'. The username is '. theme('placeholder', $url['user']) .' and the database server is '. theme('placeholder', $url['host']) .'.</p>
 <ul>
   <li>Are you sure you have the correct database name?</li>
@@ -98,14 +98,14 @@ function _db_query($query, $debug = 0) {
   }
 
   if ($debug) {
-    print '<p>query: '. $query .'<br />error:'. mysql_error() .'</p>';
+    print '<p>query: '. $query .'<br />error:'. mysql_error($active_db) .'</p>';
   }
 
-  if (!mysql_errno()) {
+  if (!mysql_errno($active_db)) {
     return $result;
   }
   else {
-    trigger_error(check_plain(mysql_error() ."\nquery: ". $query), E_USER_WARNING);
+    trigger_error(check_plain(mysql_error($active_db) ."\nquery: ". $query), E_USER_WARNING);
     return FALSE;
   }
 }
@@ -178,7 +178,8 @@ function db_result($result, $row = 0) {
  * Determine whether the previous query caused an error.
  */
 function db_error() {
-  return mysql_errno();
+  global $active_db;
+  return mysql_errno($active_db);
 }
 
 /**
@@ -203,7 +204,8 @@ function db_next_id($name) {
  * Determine the number of rows changed by the preceding query.
  */
 function db_affected_rows() {
-  return mysql_affected_rows();
+  global $active_db;
+  return mysql_affected_rows($active_db);
 }
 
 /**
@@ -311,7 +313,8 @@ function db_query_temporary($query) {
  *  Encoded data.
  */
 function db_encode_blob($data) {
-  return "'" . mysql_real_escape_string($data) . "'";
+  global $active_db;
+  return "'" . mysql_real_escape_string($data, $active_db) . "'";
 }
 
 /**
@@ -330,7 +333,8 @@ function db_decode_blob($data) {
  * Prepare user input for use in a database query, preventing SQL injection attacks.
  */
 function db_escape_string($text) {
-  return mysql_real_escape_string($text);
+  global $active_db;
+  return mysql_real_escape_string($text, $active_db);
 }
 
 /**
diff --git a/includes/database.pgsql.inc b/includes/database.pgsql.inc
index 28d5efd7a426..89e6b9d7bff1 100644
--- a/includes/database.pgsql.inc
+++ b/includes/database.pgsql.inc
@@ -85,14 +85,14 @@ function _db_query($query, $debug = 0) {
   }
 
   if ($debug) {
-    print '<p>query: '. $query .'<br />error:'. pg_last_error() .'</p>';
+    print '<p>query: '. $query .'<br />error:'. pg_last_error($active_db) .'</p>';
   }
 
   if ($last_result !== FALSE) {
     return $last_result;
   }
   else {
-    trigger_error(check_plain(pg_last_error() ."\nquery: ". $query), E_USER_WARNING);
+    trigger_error(check_plain(pg_last_error($active_db) ."\nquery: ". $query), E_USER_WARNING);
     return FALSE;
   }
 }
@@ -167,7 +167,8 @@ function db_result($result, $row = 0) {
  * Determine whether the previous query caused an error.
  */
 function db_error() {
-  return pg_last_error();
+  global $active_db;
+  return pg_last_error($active_db);
 }
 
 /**
-- 
GitLab