我有一些PDO插入代码试试catch block。我故意将错误的值传递给execute函数,以便插入失败。即; 在声明为主要的列上插入重复值。经测试,插入查询在控制台上执行时失败,提示错误:PHP try catch不抓PDOException /异常
#1062 - Duplicate entry '0' for key 'PRIMARY'
但是,我的try-catch块没有捕捉到这个异常。是否因为PHP不会为重复条目抛出异常?我是PHP新手。一直在寻找的净,但似乎无法找到一个线索:
try
{
$query = $conn->prepare($preSQL);
$query->execute($postSQL); //$postSQL is the associative array for placeholders
$dataAdded = $query->rowCount();
$lastInsertId = $this->conn->lastInsertId();
}
catch(PDOException $e)
{
fwrite($myfile,PHP_EOL);
fwrite($myfile,$e->getMessage());
fclose($myfile);
return false;
}
检查这里:https://phpdelusions.net/pdo#errors –