0

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.

  • This question is way too broad and confusing to be on topic on Stack Overflow. It is still unclear what you problem is. If you still have any questions, everything is explained i my PDO tutorial, particularly in the [running multiple queries](https://phpdelusions.net/pdo#multiquery) section. – Your Common Sense Nov 15 '19 at 08:19

1 Answers1

2

ERRMODE_WARNING causes PDO to emit warnings instead of exceptions.

If you want exceptions, use ERRMODE_EXCEPTION.

See https://www.php.net/manual/en/pdo.error-handling.php for more info.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Note that this is an error in the demo code, i wrote inside the text i use ERRMODE_EXCEPTION. – Baron Robert Nov 15 '19 at 08:05
  • Ok. But this answer explains why the demo code behaves that way. If you want an explanation for some different code, then show some different code :-) – ADyson Nov 15 '19 at 08:37