I am trying to force PDO to throw an exception when the number of bound variables is bigger than the number of params in the query. I preffer to use native prepare statements.
If i use emulated prepares (PDO::ATTR_EMULATE_PREPARES => true
) the exception works fine, i tested using this code:
$Pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT id
FROM my_table
WHERE
doc = :doc';
try {
$Ps = $Pdo->prepare($sql, array(PDO::ATTR_EMULATE_PREPARES => true));
$result = $Ps->execute(array(
':doc' => '1234',
':name' => 'Myself'
));
} catch (PDOException $e) {
throw new \Exception('Query failed: ' . $e->getMessage());
}
Throws:
Fatal error: Uncaught exception 'Exception' with message 'Query failed: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens'
But if i change PDO::ATTR_EMULATE_PREPARES
to false
, no exception is throwed, i just receive false
in $result
. For security reasons i would like to force exceptions in this case too, but i am not seeing how to reach this. Any suggestions will be welcome.
EDIT 1
Using PDO::ATTR_EMULATE_PREPARES => false
, if i do a var_dump($Ps->errorInfo())
i get:
array(3) {
[0]=>
string(5) "HY093"
[1]=>
int(7)
[2]=>
string(0) ""
}