我正在运行PHP 7.2.16
不确定启动时,纵然有错误,PDO errorCode()或errorInfo()[0]此刻老是表现00000
$pdo = new PDO('mysql:host=localhost;dbname=mydb','root','pwd');
$sth = $pdo->prepare('select now() and this is a bad SQL where a - b from c');
$sth->execute();
$row = $sth->fetchAll();
$err = $sth->errorInfo();
echo $sth->errorCode();
print_r($row);
print_r($err);
功效如下:
00000Array
(
)
Array
(
[0] => 00000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'a bad SQL where a - b from c' at line 1
)
可是,我只是做了一个新测试,通过删除$sth-> fetchAll()或在此行之前获取错误,可以正确表现:
Array
(
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'a bad SQL where a - b from c' at line 1
)
OK-办理方案是:
get the error code immediately after execute() and before any fetch
最佳谜底
我行使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().
(编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|