The PDO method fetch() returns the value FALSE both when no records are found AND on failure (e.g. when something goes wrong regarding the database access).
I need to be able to differentiate between the two situations and to handle each one in the corresponding manner:
- to display a message to the user when no records are found and
- to throw an exception on failure.
So, my question: is there a way to handle the result in a proper manner?
Thank you for your time.
P.S.: I would have expected to receive an empty array as result, when no records are found, and the value FALSE when something goes wrong. Like in the case of fetchAll() method.
UPDATE:
The method PdoStatement::fetch throws exceptions on failure, instead of FALSE. Such a case is demonstrated in my answer:
In conclusion, as @pucky124 already said, the differentiation is easy so achieve:
PDOStatement::fetchreturnsFALSEif no records are found.PDOStatement::fetchthrows exceptions on failure.