I would find useful to get the sql statement text that caused a PDOException
when catching it.
As far as I could research, the exception doesn't have that information.
For example (and after reading the docs for PDOException
class), I used Exception::__toString()
and got something like:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '14' for key 'PRIMARY'
exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '14' for key 'PRIMARY'' in xx.php:64
Stack trace:
#0 xx.php(64): PDOStatement->execute(Array)
#1 xx.php(108): insertKeplerian(Object(MyConn), '14', Object(stdClass))
#2 Command line code(1): include('/srv/www/htdocs...')
#3 {main}
The problem is that I have statements executed from different functions and I want to catch all exceptions in a single catch
block.
If it is true that the statement can't be recovered from the exception then I can think of two possible solutions:
- Storing the sql statement text in a some kind of "global" variable
than can be recovered in the
catch
section. - Catch and manage the
PDOException
in each function that executes an SQL statement
I imagine there is a better way to do this.