如何通过PDO作为整数从MYSQL获取错误代码?
try{
...
}
catch(PDOException $e){
throw new Fatal($e->getMessage(), $e->getCode());
}$e->getCode()将返回类似于HY000的内容
传递给Fatal::__construct()的参数2必须是整数类型,字符串给定. ..。->_构造(‘SQLSTATEHY000.’,'HY000')
发布于 2019-01-07 21:46:24
看看$e->errorInfo。
http://php.net/manual/en/class.pdoexception.php说:
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]元素是一个整数。
https://stackoverflow.com/questions/54081759
复制相似问题