I am trying to update my db with with PHP PDO and when issuing bad sql statements it fails without any errors or in my case exceptions.
Note that i am not using $pdo->prepare()
but instead $pdo->query()
.
I use ERRMODE_EXCEPTION option.
I read many other threads and exemples about PDO::ATTR_EMULATE_PREPARES
option (like this one.
let me write some code :
$Qs = <<<EOT
UPDATE my table where somevar = "this";
UPDATE my othertable where thisitem = "that";
INSERT INTO tablex (col1, col2,col3,invalid_col) VALUES (val1,val2,val3,val4);
INSERT INTO tabley (col1, col2,col3) VALUES (val1,val2,val3);
EOT;
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ,
/* disabled ! PDO::ATTR_EMULATE_PREPARES => false,*/
];
$pdo = new PDO(dsn,user,password, $options);
try{
$pdo->query($Qs);
catch(exception $e)
{
echo 'an error occurred' . $e->getmessage();
}
next_instruction();
In this code, that is very close to mine, exception is never thrown ! Why ? if I am not using prepare ! Note that if I enable ATTR_EMULATE_PREPARES it works well.
Can someone explain the behavior ? Should I open a bug request ?
Bye !
[EDIT]
I found some workaround to that.
1st: I split each line to an array an run the array against $pdo->query(). Then i add my exeption thrown on errors.I read somewhere that mysql was not accepting simultaneous multiple queries. Maybe this is what my sql queries where not behave correctly on error.
2nd: When I was creating new triggers in the DB, it was failing everytime with error Cannot execute queries while other unbuffered queries are active
. I was using $pdo->queries() to add them. Switching to $pdo->prepare() did the job without any issues.