Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what to do on PHP PDO execute() returns false?

Tags:

php

The php manual (http://www.php.net/manual/en/pdostatement.execute.php) mentions at Return Values: "Returns TRUE on success or FALSE on failure."

I'm writing a php script with several PDO::execute statements. What should be done when PHP:PDO would indeed return FALSE? Stop the entire script (call exit()) or continue or something else? What are possible reasons for Execute() returning false? Or is returning false only a very theoretical scenario that almost never happens in reel live?

like image 540
Joppo Avatar asked Feb 13 '23 21:02

Joppo


2 Answers

It depends on how important is the query execution for your program. There is no standard-way of handling/ignoring it.

It might help you to use the errorCode() / errorInfo() functions of PDO, so you can handle different errors on different ways.

Take a look at the first example given in the errorCode() manual page, where an execute() fails.

like image 94
Marcel Balzer Avatar answered Feb 16 '23 10:02

Marcel Balzer


At the risk of penning the impopular answer, I am not entirely sure whether checking the return value of PDOStatement::execute() is a worthwhile endeavor when PDO::ERRMODE_EXCEPTION is in use, which it should. Or, to answer your question as you formulated it, PDOStatement::execute() returning FALSE indeed appears to be quite a theoretical scenario under PDO::ERRMODE_EXCEPTION — And if it indeed does happen, your request (if not the entire Apache process) is very likely doomed anyway.

The “genuine” occurrences of RETURN_FALSE in the source code of PDOStatement::execute are few and far between. By “genuine” I mean not commented out, and not following PDO_HANDLE_STMT_ERR (which is just pdo_handle_error, which does as it says in the name). In fact, it appears that the only cases where PDOStatement::execute could possibly return FALSE (again, under the assumption that PDO::ERRMODE_EXCEPTION is set) are memory alllocation errors, which are very likely to be unrecoverable.

like image 29
DomQ Avatar answered Feb 16 '23 10:02

DomQ