Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.677
diff -u -p -r1.677 common.inc
--- includes/common.inc	12 Aug 2007 15:55:35 -0000	1.677
+++ includes/common.inc	21 Aug 2007 02:32:08 -0000
@@ -531,7 +531,7 @@ function drupal_http_request($url, $head
  *  0 = Log errors to database.
  *  1 = Log errors to database and to screen.
  */
-function drupal_error_handler($errno, $message, $filename, $line) {
+function drupal_error_handler($errno, $message, $filename, $line, $context) {
   // If the @ error suppression operator was used, error_reporting is temporarily set to 0
   if (error_reporting() == 0) {
     return;
@@ -539,6 +539,23 @@ function drupal_error_handler($errno, $m
 
   if ($errno & (E_ALL)) {
     $types = array(1 => 'error', 2 => 'warning', 4 => 'parse error', 8 => 'notice', 16 => 'core error', 32 => 'core warning', 64 => 'compile error', 128 => 'compile warning', 256 => 'user error', 512 => 'user warning', 1024 => 'user notice', 2048 => 'strict warning');
+
+    if (isset($context['query'])) {
+
+      // For queries, we want the line number/file name of the place where
+      // the query was originally called.
+      $backtrace = debug_backtrace();
+      // List of functions that could contain SQL.
+      $query_functions = array('db_query', 'pager_query');
+      foreach ($backtrace as $index => $function) {
+        if (in_array($function['function'], $query_functions)) {
+          $line = $backtrace[$index]['line'];
+          $filename = $backtrace[$index]['file'];
+          break;
+        }
+      }
+    }
+
     $entry = $types[$errno] .': '. $message .' in '. $filename .' on line '. $line .'.';
 
     // Force display of error messages in update.php
