3

Does the method PDOStatement::fetch() throw an exception on failure, if the PDO error reporting system is set to throw exceptions? E.g. if I set:

PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION

Do you know such a case?

Thank you very much.


UPDATE:

The method PdoStatement::fetch throws indeed exceptions on failure, instead of FALSE. Such a case is demonstrated in my answer:

In conclusion:

  • PDOStatement::fetch returns FALSE if no records are found.
  • PDOStatement::fetch throws exceptions on failure.
  • 1
    Your answer is in the link you provided in your question. – Nima Sep 21 '17 at 13:46
  • Usually you would have already seen an exception before you get to fetch. – Don't Panic Sep 21 '17 at 13:48
  • 2
    Why don't you try it and see if an exception is thrown – Rotimi Sep 21 '17 at 13:48
  • @Nima Thank you, Nima. I didn't found it, explicitely! I know it returns 'FALSE' on failure, but my question is: is this enough for me to 100% know, that it will throw an exception if error reporting is set on throwing exceptions? –  Sep 21 '17 at 13:51
  • @Don'tPanic Thanks. Yes, I know. But I'm VERY interested to know if `fetch()` throws one. –  Sep 21 '17 at 13:52
  • @Akintunde, your idea was/is also my idea :-) See my other two questions [Simulate a PDO fetch failure situation](https://stackoverflow.com/questions/46343196/simulate-a-pdo-fetch-failure-situation) and [PHP PDO fetch returns FALSE when no records found AND on failure](https://stackoverflow.com/questions/46345649/does-pdo-fetch-method-throw-an-exception-on-failure). It seems that is very hard to find a sure answer, that's why I posted all these three questions. –  Sep 21 '17 at 13:57
  • I've never seen it happen, but I guess that doesn't mean it won't ever happen. – Don't Panic Sep 21 '17 at 14:00
  • @Don'tPanic Exactly in these situations, which you and me and maybe all other users didn't met until now, regarding `fetch()` failure, I'm interested in. –  Sep 21 '17 at 14:05
  • Possible duplicate of [Simulate a PDO fetch failure situation](https://stackoverflow.com/questions/46343196/simulate-a-pdo-fetch-failure-situation) – pucky124 Sep 22 '17 at 14:32
  • @pucky124 No, I posted the questions separately, because one (this one) is about a confirmation, that fetch() throws exceptions, and the other one is about a failure simulation case for fetch(). Also, I just intended to come on this question now, in order to respond to the comments and revise the answers (including yours). In the light of my test from "_Simulate a PDO fetch failure situation_", I need to un-accept the answer accepted on this answer. –  Sep 22 '17 at 14:38
  • @Don'tPanic I finally found a case, which allowed me to simulate a failure situation of `PDOStatement::fetch()`. An exception was thrown. I also posted a detailed answer: [Simulate a PDO fetch failure situation](https://stackoverflow.com/questions/46343196/simulate-a-pdo-fetch-failure-situation). So, one can conclude, that `fetch()` does indeed throw exceptions on failure. Thanks again for your comments! –  Sep 22 '17 at 15:25
  • 1
    @Akintunde I finally found a case, which allowed me to simulate a failure situation of `PDOStatement::fetch()`. An exception was thrown. I also posted a detailed answer: [Simulate a PDO fetch failure situation](https://stackoverflow.com/questions/46343196/simulate-a-pdo-fetch-failure-situation). So, one can conclude, that `fetch()` does indeed throw exceptions on failure. Thanks again for your comments! –  Sep 22 '17 at 15:26

2 Answers2

-1

There is a user note on the page you mentioned in your question which says:

WARNING: fetch() does NOT adhere to SQL-92 SQLSTATE standard when dealing with empty datasets.

Instead of setting the errorcode class to 20 to indicate "no data found", it returns a class of 00 indicating success, and returns NULL to the caller.

This also prevents the exception mechainsm from firing.

Programmers will need to explicitly code tests for empty resultsets after any fetch*() instead of relying on the default behavior of the RDBMS.

I tried logging this as a bug, but it was dismissed as "working as intended". Just a head's up.

According to this note the answer to your question is NO.

Update:
Accepted answer mentions a bug which is marked as fixed and the test code provided in bug report does not result in an exception being thrown anymore. It was actually fixed back in 2007.

Nima
  • 3,309
  • 6
  • 27
  • 44
  • 1
    And I guess you should think about the meaning of the word `failure`, specially when you're fetching results which logically means `your statement has successfully executed` so there is definitely a resultset. It might be an empty one, but it exists. – Nima Sep 21 '17 at 14:16
  • I understand your point. No, by `failure` I mean a real failure. Like in the `prepare()` or `execute()` cases, when something is wrong and exceptions are thrown. –  Sep 21 '17 at 14:36
  • Hi, Nima. I am through with all the tests that I could do. The note in your answer handles only the situation of not finding records. This is not a "failure" per se, as I explained in my prior comment. But it seems indeed, that, no matter what, the error code remains always the same - 00000 - in both situations: when no records found AND on failure. And no exception is thrown. So, thank you very much for your answer. I accept it, since it's somehow the only one official and pertinent answer in the whole World Wide Web :-)) –  Sep 21 '17 at 16:39
  • I guess if the statement executes successfully then a `fetch` operation will become a task of handling an internal data structure for PDO. Hence there should not be any failures. If I face such a thing (a failure while fetching) I would look for a bug in PDO itself! – Nima Sep 21 '17 at 16:49
  • You are perfectly right !!! :-) My tests revealed, that even if mysql service is closed/killed after execute() and before fetch(), the records are displayed (though a warning is raised in the same time). So, yes, there should be no failure reasons for fetch(). –  Sep 21 '17 at 16:54
  • One last thing: fetch() never returns NULL when using PDO (as the user noted on php.net). It always returns FALSE. And the error code for "no data" is "02000", not "20". See [SQLSTATE Codes](https://docs.oracle.com/cd/A58617_01/server.804/a58231/appd.htm). I thought it would be good to know. Thanks again. Bye –  Sep 21 '17 at 17:00
  • Careful, that note only pertains to empty sets. It doesn't say anything about other failures. According to the documentation other (most likely much more important failures) would still throw an exception. – pucky124 Sep 21 '17 at 22:11
  • @Nima Hello, Nima. I'm sorry that I had to un-accept your answer. Why I did that? Because I finally found a case, in which `PDOStatement::fetch()` throws an exception on failure. I was able to test it too. So, I posted an answer for it as well. Thank you very much for your effort and advices! –  Sep 22 '17 at 14:49
  • @aendeerei Thank you for letting me know :) I'm still reading about this, in fact I'm reading php-src itself. Did you post the answer on another question? I'm eager to see what situation will cause a failure. – Nima Sep 22 '17 at 14:53
  • @Nima Oh, sorry for that :-) Of course, here it is: [Simulate a PDO fetch failure situation](https://stackoverflow.com/questions/46343196/simulate-a-pdo-fetch-failure-situation). I posted three questions yesterday, regarding the same problem with `PDOStatement::fetch()`, and I had to answer to many good advices and comments and answers. That's why, confusingly, I forgot to give a link to you :-) –  Sep 22 '17 at 15:05
-1

The user here claims that fetch() threw an exception. I'd be very careful in assuming that it doesn't or won't throw an exception just because they are typically thrown when you prepare or bind. This is a very good reason to put the call inside a try block. So to answer the question, in the highly unlikely event of failure, yes fetch() should throw an exception and in that one case it did. Now it will be interesting to see if there are other cases as well.

pucky124
  • 1,489
  • 12
  • 24
  • Hello, pucky. First of all, I appreciate your answer! You provided a good example. Unfortunately i couldn't reproduce the case. No exception was thrown in that case. Maybe because it was referring to an old PHP version. But, since my other test was successful, I definitely go with your answer, because it's correct. And I know that my question here was a bit opinionated. Sorry for that. Thank you very much for spending your time in trying to find a good solution to my question(s)! –  Sep 22 '17 at 14:58
  • 1
    At last, could you please put a reference to my other question [Simulate a PDO fetch failure situation](https://stackoverflow.com/questions/46343196/simulate-a-pdo-fetch-failure-situation) in your answer? So that all users have a living proof, that fetch() throws exceptions? Thank you. –  Sep 22 '17 at 15:00