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

查看:153
本文介绍了如何从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 Handler

PDO为MySQL启用的驱动程序
客户端API版本mysqlnd 5.0.7-dev - 091210 - $修订: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 导致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天全站免登陆