From 9a4a48a5fe5883bdb83f983b0f8b98c8ce19dc56 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Wed, 16 Oct 2013 13:14:39 +0000
Subject: [PATCH] Emit a backtrace on db_error logs

---
 include/class.osticket.php |  4 ++++
 include/mysqli.php         | 11 +++++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/include/class.osticket.php b/include/class.osticket.php
index fd46d2134..467831d5b 100644
--- a/include/class.osticket.php
+++ b/include/class.osticket.php
@@ -264,6 +264,10 @@ class osTicket {
         if($alert && !$this->getConfig()->alertONSQLError())
             $alert =false;
 
+        $e = new Exception();
+        $bt = str_replace(ROOT_DIR, '(root)/', $e->getTraceAsString());
+        $error .= "\n\n---- Backtrace ----\n".$bt;
+
         return $this->log(LOG_ERR, $title, $error, $alert);
     }
 
diff --git a/include/mysqli.php b/include/mysqli.php
index 737fd3a06..8b06a1742 100644
--- a/include/mysqli.php
+++ b/include/mysqli.php
@@ -235,8 +235,15 @@ function db_field_type($res, $col=0) {
 }
 
 function db_prepare($stmt) {
-    global $__db;
-    return $__db->prepare($stmt);
+    global $ost, $__db;
+
+    $res = $__db->prepare($stmt);
+    if (!$res && $ost) {
+        // Include a backtrace in the error email
+        $msg='['.$stmt."]\n\n".db_error();
+        $ost->logDBError('DB Error #'.db_errno(), $msg);
+    }
+    return $res;
 }
 
 function db_connect_error() {
-- 
GitLab