This code works fine, but I’ll want to handle exception if any thing goes wrong, so I deliberately made a syntax error in the query but nothing happens. Below is the code
try {
$sql = "INSERT INTO journals (topic, author, ) VALUES ('$topic', '$authors', ')";
echo "1st";
$lecturers_db->query($sql);
echo "second";
} catch(PDOException $e) {
echo $e->getMessage();
echo $msg = "Error!";
}
Without the obvious syntax error, the code works fine but with the syntax error, nothing happens, all the code in the try block executes and the code in the catch block never executes.
I want to raise an exception, please how do I do it here, thanks for any help.
Be sure to set the attribute PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION, as soon as you init your pdo object:
After that, any failed queries will raise an exception