Zend PDO 错误 - 如何调试它?

发布于 2024-09-14 16:48:18 字数 2729 浏览 7 评论 0原文

我在使用 Zend Framework 和 Mysql 时遇到一个奇怪的问题。我生成了一个查询:

SELECT events.idUser, szForename, szLastname, readers.szName, idZoneFrom, events.dtTime FROM events, users, readers WHERE events.idUser = users.idUser AND events.idReader = readers.idReader AND dtTime >= '2010:02:15 0:00:00' AND dtTime < '2010:02:16 0:00:00' ORDER BY dtTime

如果我在 pma、navicat 或 shell 客户端等控制台中运行它,查询工作正常,但是当我尝试使用通过推荐扩展 Zend_Db_Table 的模型运行它时,

$arResult = $this->getDefaultAdapter()->query($szQuery)->fetchAll();

它会出现 1064 错误:

enter code here

An error occurred
Application error
Exception information:

Message: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Stack trace:

#0 C:\xampp\htdocs\projekty\doors2\library\Zend\Db\Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array)
#1 C:\xampp\htdocs\projekty\doors2\library\Zend\Db\Adapter\Abstract.php(468): Zend_Db_Statement->execute(Array)
#2 C:\xampp\htdocs\projekty\doors2\library\Zend\Db\Adapter\Pdo\Abstract.php(238): Zend_Db_Adapter_Abstract->query('SELECT zoneName...', Array)
#3 C:\xampp\htdocs\projekty\doors2\application\models\Zones.php(24): Zend_Db_Adapter_Pdo_Abstract->query('SELECT zoneName...')
#4 C:\xampp\htdocs\projekty\doors2\application\models\Events.php(87): Application_Model_Zones->getZoneInfo(NULL)
#5 C:\xampp\htdocs\projekty\doors2\application\controllers\IndexController.php(52): Application_Model_Events->getEventsList(NULL, '02/15/2010')
#6 C:\xampp\htdocs\projekty\doors2\library\Zend\Controller\Action.php(513): IndexController->eventsAction()
#7 C:\xampp\htdocs\projekty\doors2\library\Zend\Controller\Dispatcher\Standard.php(295): Zend_Controller_Action->dispatch('eventsAction')
#8 C:\xampp\htdocs\projekty\doors2\library\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#9 C:\xampp\htdocs\projekty\doors2\library\Zend\Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch()
#10 C:\xampp\htdocs\projekty\doors2\library\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#11 C:\xampp\htdocs\projekty\doors2\public\index.php(26): Zend_Application->run()
#12 {main}  

Request Parameters:

array (
  'controller' => 'index',
  'action' => 'events',
  'module' => 'default',
  'idUser' => '0',
  'dt' => '02/15/2010',
)  

How can I debug it to找到问题了??或者也许你知道我做错了什么?

附:我使用相同的数据库用户进行 php 和查询测试......

I have a weird problem using Zend Framework and Mysql. I generated a query:

SELECT events.idUser, szForename, szLastname, readers.szName, idZoneFrom, events.dtTime FROM events, users, readers WHERE events.idUser = users.idUser AND events.idReader = readers.idReader AND dtTime >= '2010:02:15 0:00:00' AND dtTime < '2010:02:16 0:00:00' ORDER BY dtTime

The query works ok if I run it in some console like pma, navicat or shell client but when I try to run it using a model that extends Zend_Db_Table by commend

$arResult = $this->getDefaultAdapter()->query($szQuery)->fetchAll();

it goes with 1064 error:

enter code here

An error occurred
Application error
Exception information:

Message: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Stack trace:

#0 C:\xampp\htdocs\projekty\doors2\library\Zend\Db\Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array)
#1 C:\xampp\htdocs\projekty\doors2\library\Zend\Db\Adapter\Abstract.php(468): Zend_Db_Statement->execute(Array)
#2 C:\xampp\htdocs\projekty\doors2\library\Zend\Db\Adapter\Pdo\Abstract.php(238): Zend_Db_Adapter_Abstract->query('SELECT zoneName...', Array)
#3 C:\xampp\htdocs\projekty\doors2\application\models\Zones.php(24): Zend_Db_Adapter_Pdo_Abstract->query('SELECT zoneName...')
#4 C:\xampp\htdocs\projekty\doors2\application\models\Events.php(87): Application_Model_Zones->getZoneInfo(NULL)
#5 C:\xampp\htdocs\projekty\doors2\application\controllers\IndexController.php(52): Application_Model_Events->getEventsList(NULL, '02/15/2010')
#6 C:\xampp\htdocs\projekty\doors2\library\Zend\Controller\Action.php(513): IndexController->eventsAction()
#7 C:\xampp\htdocs\projekty\doors2\library\Zend\Controller\Dispatcher\Standard.php(295): Zend_Controller_Action->dispatch('eventsAction')
#8 C:\xampp\htdocs\projekty\doors2\library\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#9 C:\xampp\htdocs\projekty\doors2\library\Zend\Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch()
#10 C:\xampp\htdocs\projekty\doors2\library\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#11 C:\xampp\htdocs\projekty\doors2\public\index.php(26): Zend_Application->run()
#12 {main}  

Request Parameters:

array (
  'controller' => 'index',
  'action' => 'events',
  'module' => 'default',
  'idUser' => '0',
  'dt' => '02/15/2010',
)  

How can I debug it to find the problem?? Or maybe you know what could I have done wrong?

ps. I use the same db user both for php and query testing...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

森林迷了鹿 2024-09-21 16:48:18

正如我所看到的,你的 SQL 语句有保留字。您应该尝试构建您的连接,以便它可以自动转义标识符,或者您应该自己手动进行。对于第一个选项,请尝试以下操作:

  $options = array(Zend_Db::AUTO_QUOTE_IDENTIFIERS => false);
  $params = array(
      'host'           => 'host',
      'username'       => 'user',
      'password'       => 'secret',
      'dbname'         => 'db',
      'options'        => $options
  );
  $db = Zend_Db::factory('Pdo_Mysql', $params);

Your SQL statement have reserved words, as I can see. You should try to build your conection so it can automatically escape identifiers or you should do it your self, manually. For the first option, try this:

  $options = array(Zend_Db::AUTO_QUOTE_IDENTIFIERS => false);
  $params = array(
      'host'           => 'host',
      'username'       => 'user',
      'password'       => 'secret',
      'dbname'         => 'db',
      'options'        => $options
  );
  $db = Zend_Db::factory('Pdo_Mysql', $params);
烙印 2024-09-21 16:48:18

如果将查询分成多行,您可能会获得更有用的行号来了解语法错误的位置。这就是我格式化查询的方式。它使它们更容易阅读、维护和调试。

SELECT
    events.idUser,
    szForename,
    szLastname,
    readers.szName,
    idZoneFrom,
    events.dtTime
FROM events,
    users,
    readers
WHERE events.idUser = users.idUser
    AND events.idReader = readers.idReader
    AND dtTime >= '2010:02:15 0:00:00'
    AND dtTime < '2010:02:16 0:00:00'
ORDER BY dtTime

If you break your query into multiple lines you may get a more useful line number for where the syntax error is. This is how I format my queries. It makes them much easier to read, maintain and debug.

SELECT
    events.idUser,
    szForename,
    szLastname,
    readers.szName,
    idZoneFrom,
    events.dtTime
FROM events,
    users,
    readers
WHERE events.idUser = users.idUser
    AND events.idReader = readers.idReader
    AND dtTime >= '2010:02:15 0:00:00'
    AND dtTime < '2010:02:16 0:00:00'
ORDER BY dtTime
極樂鬼 2024-09-21 16:48:18

另外,对于权限,mysql使用基于用户+主机的权限系统。因此,某个用户可能可以从本地主机访问数据库资源,但不能从其他系统访问数据库资源。

检查 http://dev.mysql.com/doc/refman/5.0/ en/grant.html 了解如何更改 mysql 用户权限的信息。

Also, as for permissions, mysql uses a user+host based permission system. So a certain user may have access to a database resource from localhost, but not from other systems.

Check http://dev.mysql.com/doc/refman/5.0/en/grant.html for information on how to alter mysql user permissions.

沩ん囻菔务 2024-09-21 16:48:18

如果您使用 zend,您至少应该准备好传入的任何值。如果该语句在命令行中工作,那么保留字不应该有问题(除非由于某种原因 mysql 的版本不同,课程)。

If you are using zend, you should be at least preparing any values that are passed in. If the statement works in the command line then there should not be a problem with reserved words (unless for some reason the versions of mysql are distinct, of course).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文