php pdo 00000,php-即使有错误,PDO错误代码也总是00000
我使用PHP 7.1.23測試了以下代碼:
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,true);
$sth = $pdo->prepare('select now() and this is a bad sql where a - b from c');
if ($sth === false) {
echo "error on prepare()\n";
print_r($pdo->errorInfo());
}
if ($sth->execute() === false) {
echo "error on execute()\n";
print_r($sth->errorInfo());
}
error on execute()
Array
(
[0] => 42000
[1] => 1064
[2] => 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 'a bad sql where a - b from c' at line 1
)
然后,我測試了相同的代碼,除非禁用了仿真的prepare:
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
error on prepare()
Array
(
[0] => 42000
[1] => 1064
[2] => 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 'a bad sql where a - b from c' at line 1
)
Fatal error: Uncaught Error: Call to a member function execute() on boolean
故事的道德啟示:
>使用模擬的準備好的語句時,prepare()是空操作,并且錯誤會延遲到execute()為止.我建議禁用模擬的prepare,除非您使用的數據庫不支持prepared語句(我不知道任何RDBMS產品的任何當前版本都不能執行真正的prepared語句).
>在prepare()上檢查錯誤時,請使用$pdo-> errorInfo().
>在execute()上檢查錯誤時,請使用$stmt-> errorInfo().
總結
以上是生活随笔為你收集整理的php pdo 00000,php-即使有错误,PDO错误代码也总是00000的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端lvs访问多台nginx代理服务时出
- 下一篇: 动态规划算法php,php算法学习之动态