【发布时间】:2017-02-27 11:39:05
【问题描述】:
我在 try catch 块中有一些 PDO 插入代码。我故意将错误的值传递给执行函数,以便插入失败。 IE。; 在声明为主要的列上插入重复值。经测试,在控制台上执行插入查询失败,出现错误:
#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;
}
【问题讨论】: