From 38f439708cd78b224978c3d3d9cd85d40a9f9c04 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 2 Aug 2013 16:20:39 +0400 Subject: [PATCH] rollback current transaction before trying to report SQL query errors, properly save database error messages when logging query errors --- classes/db/mysql.php | 7 +++++-- classes/db/mysqli.php | 7 +++++-- classes/db/pgsql.php | 7 +++++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/classes/db/mysql.php b/classes/db/mysql.php index aab05aca..d4b45b98 100644 --- a/classes/db/mysql.php +++ b/classes/db/mysql.php @@ -26,9 +26,12 @@ class Db_Mysql implements IDb { } function query($query, $die_on_error = true) { - $result = mysql_query($query, $this->link); + $result = @mysql_query($query, $this->link); if (!$result) { - user_error("Query $query failed: " . ($this->link ? mysql_error($this->link) : "No connection"), + $error = @mysql_error($this->link); + + @mysql_query("ROLLBACK", $this->link); + user_error("Query $query failed: " . ($this->link ? $error : "No connection"), $die_on_error ? E_USER_ERROR : E_USER_WARNING); } return $result; diff --git a/classes/db/mysqli.php b/classes/db/mysqli.php index a41ebf8e..c685b75a 100644 --- a/classes/db/mysqli.php +++ b/classes/db/mysqli.php @@ -24,9 +24,12 @@ class Db_Mysqli implements IDb { } function query($query, $die_on_error = true) { - $result = mysqli_query($this->link, $query); + $result = @mysqli_query($this->link, $query); if (!$result) { - user_error("Query $query failed: " . ($this->link ? mysqli_error($this->link) : "No connection"), + $error = @mysqli_error($this->link); + + @mysqli_query($this->link, "ROLLBACK"); + user_error("Query $query failed: " . ($this->link ? $error : "No connection"), $die_on_error ? E_USER_ERROR : E_USER_WARNING); } diff --git a/classes/db/pgsql.php b/classes/db/pgsql.php index 4d860790..ba37f83a 100644 --- a/classes/db/pgsql.php +++ b/classes/db/pgsql.php @@ -35,11 +35,14 @@ class Db_Pgsql implements IDb { } function query($query, $die_on_error = true) { - $result = pg_query($query); + $result = @pg_query($this->link, $query); if (!$result) { + $error = @pg_last_error($this->link); + + @pg_query($this->link, "ROLLBACK"); $query = htmlspecialchars($query); // just in case - user_error("Query $query failed: " . ($this->link ? pg_last_error($this->link) : "No connection"), + user_error("Query $query failed: " . ($this->link ? $error : "No connection"), $die_on_error ? E_USER_ERROR : E_USER_WARNING); } return $result; -- 2.39.5