php 如何从 PDO 中挤出错误信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3726505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to squeeze error message out of PDO?
提问by Your Common Sense
I can't seem to get any error message from PDO:
我似乎无法从 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();
}
It is giving out only:
它只发出:
PDOStatement Object
(
[queryString] => @$%T$!!!
)
Array
(
[0] => 00000
[1] =>
[2] =>
)
setAttribute doesn't help anything.
setAttribute 没有任何帮助。
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 $
它是 PHP 5.3.3 Apache 2.0 Handler
PDO Driver for MySQL enabled
Client API version mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $
What can I do to get error information?
如何获取错误信息?
回答by Pekka
setAttribute willcause PDO to throw up errors or exceptions - the latest when you executethe query.
setAttribute将导致 PDO 抛出错误或异常 -执行查询时的最新情况。
For emulated prepared statements, there is no check in prepare()
:
对于模拟的准备好的语句,没有签入prepare()
:
Emulated prepared statements does not communicate with the database server so PDO::prepare() does not check the statement.
模拟的预处理语句不与数据库服务器通信,因此 PDO::prepare() 不检查语句。
But there will be one in execute()
when the query gets sent to the server.
但是execute()
当查询被发送到服务器时会有一个。
However, the mySQL driver supports native prepared statements since mySQL 4.1 anyway, so this shouldn't apply. Using
但是,无论如何,从 mySQL 4.1 开始,mySQL 驱动程序都支持本机准备好的语句,因此这不应该适用。使用
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
mustcause an exception for the query you use.
必须导致您使用的查询出现异常。
回答by CINCHAPPS
I too was trying to get the information from errorInfo()
at the database handle level, but I ended up getting the information from the statement level with PDOStatement::errorInfo()
我也试图从errorInfo()
数据库句柄级别获取信息,但我最终从语句级别获取了信息PDOStatement::errorInfo()
Per PHP web site:
每个 PHP 网站:
PDO::errorInfo() only retrieves error information for operations performed directly on the database handle. If you create a PDOStatement object through PDO::prepare() or PDO::query() and invoke an error on the statement handle, PDO::errorInfo() will not reflect the error from the statement handle. You must call PDOStatement::errorInfo() to return the error information for an operation performed on a particular statement handle.
PDO::errorInfo() 仅检索直接在数据库句柄上执行的操作的错误信息。如果您通过 PDO::prepare() 或 PDO::query() 创建 PDOStatement 对象并调用语句句柄上的错误,PDO::errorInfo() 将不会反映语句句柄中的错误。您必须调用 PDOStatement::errorInfo() 来返回对特定语句句柄执行的操作的错误信息。
回答by aimme
This will print error code as well its corresponding detailed message.
这将打印错误代码及其相应的详细消息。
Advice: this is just a demonstration. Just use for debugging purpose. Don't enable to show error messages to the public in a release version.
忠告:这只是一个示范。仅用于调试目的。不要启用在发布版本中向公众显示错误消息。
try{
connection=$this->get_connection();//here i brought my connection string
connection->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
connection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
/**
Do your works here..
//$statement=$connection->prepare($sql);
//if you are using errorInfo use after prepare statement before execute.here in this method i am not using it.
//print_r($statement->errorInfo());
**/
$statement->execute();
}
catch(PDOException $e) {
//this will echo error code with detail
//example: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'nasme' in 'field list'
echo $e->getMessage();
}
//$statement=null;
回答by shamittomar
You need to first execute
the query and then check for errors: So do it like this:
您需要先execute
查询,然后检查错误: 所以这样做:
$sth->execute();
and then check for errors. Then you will get errors, if any.
然后检查错误。然后你会得到错误,如果有的话。