Php 即使出现错误,PDO错误代码始终为00000

Php 即使出现错误,PDO错误代码始终为00000,php,mysql,pdo,Php,Mysql,Pdo,我正在运行PHP7.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 = $s

我正在运行PHP7.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
)
好的-解决方案是:

execute()
之后和任何提取之前立即获取错误代码


我用PHP7.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
)
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
然后,我测试了相同的代码,除了禁用模拟准备之后:

$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
输出:

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
)
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
故事的寓意:

  • 当使用模拟的prepared语句时,
    prepare()
    是一个no-op,错误会延迟到
    execute()
    。我建议禁用模拟prepare,除非您使用的数据库不支持prepared语句(我不知道任何当前版本的RDBMS产品都不能执行真正的prepared语句)

  • 检查prepare()上的错误时,请使用
    $pdo->errorInfo()

  • 在execute()上检查错误时,请使用
    $stmt->errorInfo()


什么是“一个”/“另一个”?我只看到一个代码。您是否检查了
$pdo
中的错误?谢谢。出现错误是因为在fetchAll之前运行了错误检查等:)能否在errorInfo()之前使用fetchAll重新测试?从失败的查询中获取fetchAll()没有任何意义。您应该在prepare()或execute()之后立即检查错误。如果禁用模拟prepare,您甚至无法尝试fetchAll(),因为
$sth
将为false,并且您无法从中调用方法。是的。我仔细检查了SIDU,这是在提取之前检查错误。谢谢