如何在 PDO PHP 中查看查询错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8776344/
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 view query error in PDO PHP
提问by TPSstar
try {
$db = new PDO("mysql:host=".HOST.";dbname=".DB, USER, PW);
$st = $db->prepare("SELECT * FROM c6ode");
}
catch (PDOException $e){
echo $e->getMessage();
}
How can I check the mysql error for the query in above case?
在上述情况下,如何检查查询的 mysql 错误?
回答by VolkerK
You need to set the error mode attribute PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION.
And since you expect the exception to be thrown by the prepare() method you should disable the PDO::ATTR_EMULATE_PREPARES* feature. Otherwise the MySQL server doesn't "see" the statement until it's executed.
您需要将错误模式属性 PDO::ATTR_ERRMODE 设置为 PDO::ERRMODE_EXCEPTION。
并且由于您希望 prepare() 方法抛出异常,因此您应该禁用 PDO::ATTR_EMULATE_PREPARES*功能。否则 MySQL 服务器不会“看到”语句,直到它被执行。
<?php
try {
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->prepare('INSERT INTO DoesNotExist (x) VALUES (?)');
}
catch(Exception $e) {
echo 'Exception -> ';
var_dump($e->getMessage());
}
prints (in my case)
打印(在我的情况下)
Exception -> string(91) "SQLSTATE[42S02]: Base table or view not found:
1146 Table 'test.doesnotexist' doesn't exist"
see http://wezfurlong.org/blog/2006/apr/using-pdo-mysql/
EMULATE_PREPARES=true seems to be the default setting for the pdo_mysql driver right now.
The query cache thing has been fixed/change since then and with the mysqlnd driver I hadn't problems with EMULATE_PREPARES=false (though I'm only a php hobbyist, don't take my word on it...)
参见http://wezfurlong.org/blog/2006/apr/using-pdo-mysql/
EMULATE_PREPARES=true 现在似乎是 pdo_mysql 驱动程序的默认设置。从那时起,查询缓存的内容已得到修复/更改,并且使用 mysqlnd 驱动程序我没有遇到 EMULATE_PREPARES=false 问题(虽然我只是一个 php 爱好者,不要相信我的话......)
*) and then there's PDO::MYSQL_ATTR_DIRECT_QUERY- I must admit that I don't understand the interaction of those two attributes (yet?), so I set them both, like
*) 然后是PDO::MYSQL_ATTR_DIRECT_QUERY- 我必须承认我不理解这两个属性的交互(还没有?),所以我设置了它们,比如
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly', array(
PDO::ATTR_EMULATE_PREPARES=>false,
PDO::MYSQL_ATTR_DIRECT_QUERY=>false,
PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
));
回答by ladar
I'm using this without any additional settings:
我在没有任何额外设置的情况下使用它:
if (!$st->execute()) {
print_r($st->errorInfo());
}
回答by Steve Rukuts
I'm guessing that your complaint is that the exception is not firing. PDO is most likely configured to not throw exceptions. Enable them with this:
我猜你的抱怨是异常没有触发。PDO 很可能被配置为不抛出异常。使用以下方法启用它们:
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
回答by Lan
a quick way to see your errors whilst testing:
在测试时快速查看错误的方法:
$error= $st->errorInfo();
echo $error[2];
回答by Timothy Nwanwene
/* Provoke an error -- the BONES table does not exist */
/* 引发错误 -- BONES 表不存在 */
$sth = $dbh->prepare('SELECT skull FROM bones');
$sth->execute();
echo "\nPDOStatement::errorInfo():\n";
$arr = $sth->errorInfo();
print_r($arr);
output
输出
Array
(
[0] => 42S02
[1] => -204
[2] => [IBM][CLI Driver][DB2/LINUX] SQL0204N "DANIELS.BONES" is an undefined name. SQLSTATE=42704
)