如何从 PDO 中挤出错误信息? [英] How to squeeze error message out of PDO?

查看:34
本文介绍了如何从 PDO 中挤出错误信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法从 PDO 收到任何错误消息:

I can't seem to get any error message from PDO:

#$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
try {
  $sth = $dbh->prepare('@$%T$!!!');
  print_r($sth);
  print_r($dbh->errorInfo());
} catch (PDOException $e) {
    echo $e->getMessage();
}

它只发出:

PDOStatement Object
(
    [queryString] => @$%T$!!!
)
Array
(
    [0] => 00000
    [1] =>
    [2] =>
)

setAttribute 没有任何帮助.

setAttribute doesn't help anything.

这是 PHP 5.3.3 Apache 2.0 处理程序
已启用 MySQL 的 PDO 驱动程序
客户端 API 版本 mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $

It's PHP 5.3.3 Apache 2.0 Handler
PDO Driver for MySQL enabled
Client API version mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $

如何获取错误信息?

推荐答案

setAttribute will 导致 PDO 抛出错误或异常 - 当您执行查询时的最新情况.

setAttribute will cause PDO to throw up errors or exceptions - the latest when you execute the query.

对于模拟的预处理语句,prepare() 中没有检查:

For emulated prepared statements, there is no check in prepare():

模拟的预处理语句不与数据库服务器通信,因此 PDO::prepare() 不检查语句.

Emulated prepared statements does not communicate with the database server so PDO::prepare() does not check the statement.

但是当查询被发送到服务器时,execute() 中会有一个.

But there will be one in execute() when the query gets sent to the server.

但是,无论如何,mySQL 驱动程序从 mySQL 4.1 开始就支持本机准备好的语句,因此这不应该适用.使用

However, the mySQL driver supports native prepared statements since mySQL 4.1 anyway, so this shouldn't apply. Using

$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

必须 导致您使用的查询出现异常.

must cause an exception for the query you use.

这篇关于如何从 PDO 中挤出错误信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆