0

当我的站点上发生应用程序错误并且它与格式错误的 SQL 查询连接时,我想知道解决问题的查询。但这并不容易,当出现这样的错误时,给出的信息是这样的:

发生错误

应用程序错误

异常信息:

消息:SQLSTATE [42S22]:未找到列:1054 'where 子句'中的未知列 'p.name'

堆栈跟踪:

#0 /var/www/ksiegarnia/library/Zend/Db/Statement.php(303): Zend_Db_Statement_Pdo->_execute(Array)
#1 /var/www/ksiegarnia/library/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#2 /var/www/ksiegarnia/library/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query(Object(Zend_Db_Table_Select), Array)
#3 /var/www/ksiegarnia/library/Zend/Db/Select.php(686): Zend_Db_Adapter_Pdo_Abstract->query(Object(Zend_Db_Table_Select))
#4 /var/www/ksiegarnia/library/Zend/Paginator/Adapter/DbSelect.php(142): Zend_Db_Select->query(2)
#5 /var/www/ksiegarnia/library/Zend/Paginator/Adapter/DbSelect.php(183): Zend_Paginator_Adapter_DbSelect->setRowCount(Object(Zend_Db_Table_Select))
#6 [internal function]: Zend_Paginator_Adapter_DbSelect->count()
#7 /var/www/ksiegarnia/library/Zend/Paginator.php(540): count(Object(Zend_Paginator_Adapter_DbTableSelect))
#8 /var/www/ksiegarnia/library/Zend/Paginator.php(1081): Zend_Paginator->getTotalItemCount()
#9 /var/www/ksiegarnia/library/Zend/Paginator.php(753): Zend_Paginator->_calculatePageCount()
#10 /var/www/ksiegarnia/application/modules/default/controllers/IndexController.php(702): Zend_Paginator->setItemCountPerPage(10)
#11 /var/www/ksiegarnia/library/Zend/Controller/Action.php(516): IndexController->searchadvancedAction()
#12 /var/www/ksiegarnia/library/Zend/Controller/Dispatcher/Standard.php(308): Zend_Controller_Action->dispatch('searchadvancedA...')
#13 /var/www/ksiegarnia/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#14 /var/www/ksiegarnia/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#15 /var/www/ksiegarnia/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#16 /var/www/ksiegarnia/public_html/index.php(32): Zend_Application->run()
#17 {main}  

如何记录导致错误的 SQL 查询?

4

1 回答 1

0

我找到了一个相当复杂但有用的解决方案,将以下代码添加到 ErrorController.php:当异常属于 Zend_Db_Statement_Exception 类时,转到特定的跟踪行,获取在那里使用的 Zend_Db_Select 参数并获取部分SQL 子句(where、from、order、group 等),并将它们记录到日志文件中。恶心,不是吗?;) 但有效 :)

这是要插入的代码ErrorController.php

    $thread_id = exec('echo $$');

    if (get_class($errors->exception) == 'Zend_Db_Statement_Exception') {
      $trace = $errors->exception->getTrace();

      foreach(array('columns', 'from', 'where', 'order', 'group', 'limitcount', 'limitoffset', 'union') as $queryPartName) {
        $parts = $trace[2]['args'][0]->getPart($queryPartName);
        if ((is_array($parts) && !empty($parts)) ||
            (!is_array($parts) && trim($parts) !== '')) {
          $log->log($thread_id. "\t".strtoupper($queryPartName) .': ', $priority);
          if (is_array($parts)) {
            foreach($parts as $part)
              $log->log($thread_id. "\t"."\t". (is_array($part) ? 'Array: '. implode(', ', $part) : $part), $priority);
          } else
            $log->log($thread_id. "\t"."\t". $parts, $priority);
        }
      }

    }
于 2013-05-20T15:19:26.227 回答