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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With