4

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?

Joppo
  • 715
  • 2
  • 12
  • 31
  • 1
    It might return false if you try to make a change that is not allowed by the database schema, because of a constraint like unique value, foreign key, duplicate entry, etc. – jgroenen Feb 06 '14 at 13:29

3 Answers3

5

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.

Maarkoize
  • 2,601
  • 2
  • 16
  • 34
3

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.

DomQ
  • 4,184
  • 38
  • 37
  • 2
    PDOStatement::execute() returned false for me when I tried to use a named placeholder twice. PDO::errorInfo() was not helpful in figuring this out because it only gave me information about the previous valid query, – BladePoint Mar 01 '20 at 16:58
2

It really depends on what kind of query you execute. If it's a part of a transaction, you might want to rollback it. If it is crucial to subsequent program execution, you will surely want to stop it and display an error message. If your program can live without it, you can just ignore it or handle in any way you believe is right.

It returns false when there was an error. It might happen due to lack of connection, improper parameters etc.

Shocked
  • 627
  • 4
  • 13
  • thnx. I believe I read somewhere (I'm not a sql expert) that there are some restrictions on using COMMIT and ROLLBACK (?) with MySQL but I have to look that up again. I do conclude from your answer that implementing error management is recommended anyway... – Joppo Feb 06 '14 at 13:44
  • There are different storage engines and they might handle transactions differently. I'm no expert on this topic either but I haven't encoutered any problems with transactions in mysql myself. Error management surely is a good way to go. However the best way to implement it still depends on who and how will use it. – Shocked Feb 06 '14 at 15:14