|
Errors and error handlingPDO offers you a choice of 3 different error handling strategies, to fit your style of application development.
PDO standardizes on using SQL-92 SQLSTATE error code strings; individual PDO drivers are responsible for mapping their native codes to the appropriate SQLSTATE codes. The PDO::errorCode method returns a single SQLSTATE code. If you need more specific information about an error, PDO also offers an PDO::errorInfo method which returns an array containing the SQLSTATE code, the driver specific error code and driver specific error string.
Example #1 Create a PDO instance and set the error mode
The above example will output: Fatal error: Uncaught PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'testdb.wrongtable' doesn't exist in /tmp/pdo_test.php:10 Stack trace: #0 /tmp/pdo_test.php(10): PDO->query('SELECT wrongcol...') #1 {main} thrown in /tmp/pdo_test.php on line 10
Example #2 Create a PDO instance and set the error mode from the constructor
The above example will output: Warning: PDO::query(): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.wrongtable' doesn't exist in /tmp/pdo_test.php on line 9 |