I have some PDO insert code inside try catch block. I am deliberately passing wrong values to the execute function so that the insert will fail. i.e.; inserting duplicate values on a column that is declared as primary. As tested the insert query fails while executing on console, giving error:
#1062 - Duplicate entry '0' for key 'PRIMARY'
However, my try--catch block is not catching this exception. Is it because PHP does not throw an exception for duplicate entry? I am new to PHP. Have been searching the net but cannot seem to find a clue:
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;
}