PDO does not throw an exception from an invalid SQL query

I am very familiar with MySQLi and tried PDO, I heard that it is better. I looked through the tutorial below. They say that PDO throws an exception when PDO :: ERRMODE_EXCEPTION is set, and we accidentally do something wrong with the query (for example, typing DELET instead of SELECT incorrectly). I typed the same invalid request to see the error and exception message in my local environment. I have the latest WAMP installed with PHP 5.5, MySQL 5.6 and Apache 2.4.9 on my 32-bit Windows 7 PC, but didnโ€™t get what I expected, there were no exceptions. I tried the same code that was posted in the tutorial:

try { $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); # UH-OH! Typed DELECT instead of SELECT! $DBH->prepare('DELECT name FROM people'); } catch(PDOException $e) { echo "I'm sorry, Dave. I'm afraid I can't do that."; file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND); } 

No PDOError.txt, no error message. Is there something wrong with the tutorial or my environment? Or are there cases where PDO cannot throw exceptions? I have the PDO driver for MySQL installed.

+5
source share
2 answers

This is probably not the case, because the request is never executed and awaits further instructions.

Since it shows that nothing works, then all this acts in the eyes of the PDO; this is my analogy about this.

Run it and you will see that it will throw an exception.

Either by executing it after preparation, or simply ->query() , and not ->prepare() .

+5
source

This is actually related to another attribute of the PDO connection, which you may not have considered yet. And this is PDO::ATTR_EMULATE_PREPARES

If you add this attribute and set it to false , it will report that the PDO extension will issue prepare to the database for compilation planning, optimization, and planning at the moment you issue ->prepare() . If you leave it unchecked, it will default to true , which tells the EMULATE compilation extension. In other words, it will wait until you issue ->execute() before compiling the statement and error messages.

PDO :: ATTR_EMULATE_PREPARES Enables or disables emulation of prepared statements. Some drivers do not support native prepared statements or have limited support for them. Use this parameter to force the PDO to either always imitate prepared statements (if TRUE), or try to use its own prepared statements (if FALSE). It will always return to emulation of the prepared statement if the driver cannot successfully prepare the current request.

So, run your code with this optional Attribute parameter and you will see the difference.

 try { $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $DBH->setAttribute( PDO::ATTR_EMULATE_PREPARES, FALSE ); # UH-OH! Typed DELECT instead of SELECT! $DBH->prepare('DELECT name FROM people'); } catch(PDOException $e) { echo "I'm sorry, Dave. I'm afraid I can't do that."; file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND); } 

Now you will receive a message and the file will be created with an error message

+5
source

Source: https://habr.com/ru/post/1241535/


All Articles