Я использую 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- решение состоит в том, что:
получить код ошибки сразу после execute()
и перед любой выборкой