Easy Tutorial
❮ Pdostatement Errorcode Func Array Uintersect Uassoc ❯

PHP PDO Errors and Error Handling

PHP PDO Reference Manual

This is the default mode. PDO will simply set the error code, which can be checked using the PDO::errorCode() and PDO::errorInfo() methods for both statement and database objects. If the error was generated by a call to the statement object, you can call PDOStatement::errorCode() or PDOStatement::errorInfo() on that object. If the error was generated by a call to the database object, you can call the aforementioned methods on the database object.

In addition to setting the error code, PDO will emit a traditional E_WARNING message. This setting is useful during debugging/testing if you just want to see what problems are occurring without interrupting the application flow.

In addition to setting the error code, PDO will throw a PDOException exception class and set its properties to reflect the error code and information. This setting is also useful during debugging as it effectively magnifies the point where an error is generated in the script, allowing you to quickly pinpoint potential problem areas in your code (remember: if an exception causes the script to terminate, the transaction is automatically rolled back).

Another useful aspect of exception mode is that it allows for clearer construction of your own error handling compared to traditional PHP-style warnings, and requires less code/nesting than silent mode with explicit checks of return values for every database call.

Creating a PDO Instance and Setting the Error Mode

<?php
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
?>

Note: Regardless of whether PDO::ATTR_ERRMODE is currently set, if the connection fails, PDO::__construct() will always throw a PDOException exception. Uncaught exceptions are fatal.

Creating a PDO Instance and Setting the Error Mode in the Constructor

<?php
$dsn = 'mysql:dbname=test;host=127.0.0.1';
$user = 'googleguy';
$password = 'googleguy';

/*
    Using try/catch around the constructor is still effective even if ERRMODE is set to WARNING,
    because if the connection fails, PDO::__construct will always throw a PDOException exception.
*/
try {
    $dbh = new PDO($dsn, $user, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
    exit;
}

// This will cause PDO to throw an E_WARNING level error instead of an exception (when the table does not exist)
$dbh->query("SELECT wrongcolumn FROM wrongtable");
?>

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 18
add a note add a note

PHP PDO Reference Manual

❮ Pdostatement Errorcode Func Array Uintersect Uassoc ❯