Often it can be quite hard to debug SQL errors when using PHP since PHP will often throw a generic error that doesn’t really help you diagnose the situation. Other times you may not want to allow PHP errors to be shown. A quick way to debug PDO SQL errors is to use the or die function. “or die” works by executing the SQL query and if it fails to execute it will “die” and output the contents of the function. This is quite similar to a try catch block.
In order to output the PDO error you will need to get the error info from the statement object. To implement this function simply add the the or die code to the end of the $stmt->execute code.
$stmt = $db->prepare($sql);
$stmt->execute($array) or die(print_r($stmt->errorInfo(), true));