pdo get sql错误代码-必须是integer类型

0wi1tuuw  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(365)

如何通过pdo从mysql获取整数错误码?

try{
    ...
}
catch(PDOException $e){
    throw new Fatal($e->getMessage(), $e->getCode());
}
``` `$e->getCode()` 将返回类似 `HY000` 传递给致命参数::\uu construct()的参数2必须是integer类型,字符串给定。。。
... 致命->构造('sqlstate[hy000]…','hy000')
ev7lccsx

ev7lccsx1#

看一看 $e->errorInfo .
http://php.net/manual/en/class.pdoexception.php 说:
错误信息
对应于pdo::errorinfo()或pdostatement::errorinfo()
http://php.net/manual/en/pdostatement.errorinfo.php 记录返回的字段 errorInfo() .
例子:

try {
        $stmt = $pdo->query("Bogus Query");
} catch (PDOException $e) {
        echo "Caught exception!\n";
        var_dump($e->errorInfo);
}

输出:

Caught exception!
array(3) {
  [0]=>
  string(5) "42000"
  [1]=>
  int(1064)
  [2]=>
  string(157) "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 'Bogus Query' at line 1"
}

你可以看到 [1] 元素是整数。

相关问题