PHP Doku:: The PDOException class - class.pdoexception.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDatenbankerweiterungenAbstraktionsebenenPHP Data ObjectsThe PDOException class

Ein Service von Reinhard Neidl - Webprogrammierung.

PHP Data Objects


PDO Treiber>>

The PDOException class


Stellt einen Fehler dar, der von PDO ausgelöst wird. Sie sollten selbst keine PDOException in Ihrem eigenen Code werfen. Unter Ausnahmen finden Sie weitere Informationen über Ausnahmen in PHP.


PDOException extends RuntimeException {
/* Properties */
public string $errorInfo ;
protected string $message ;
protected string $code ;
/* Geerbte Methoden */
final public string Exception::getMessage ( void )
final public Exception Exception::getPrevious ( void )
final public int Exception::getCode ( void )
final public string Exception::getFile ( void )
final public int Exception::getLine ( void )
final public array Exception::getTrace ( void )
final public string Exception::getTraceAsString ( void )
public string Exception::__toString ( void )
final private void Exception::__clone ( void )



Corresponds to PDO::errorInfo() or PDOStatement::errorInfo()


Textual error message. Exception::getMessage() to access it.


SQLSTATE error code. Use Exception::getCode() to access it.

3 BenutzerBeiträge:
- Beiträge aktualisieren...
Marco01_809 "at" web "dot" de
21.08.2010 14:06
This is a PDOException Error handler for Using/Debugging in Web Browser.

echo '<html><head><title>PHP PDO Tests</title></head><body>';

// Try the following code:
try {
$db = new PDO('sqlite:database.sdb'); // Create a new PDO Object
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Set Errorhandling to Exception

$db->query("BlaBlaBla"); // Query something wrong

$db = null; // "Disconnect"
catch (
PDOException $err) {
// Catch Expcetions from the above code for our Exception Handling
$trace = '<table border="0">';
    foreach (
$err->getTrace() as $a => $b) {
        foreach (
$b as $c => $d) {
            if (
$c == 'args') {
                foreach (
$d as $e => $f) {
$trace .= '<tr><td><b>' . strval($a) . '#</b></td><td align="right"><u>args:</u></td> <td><u>' . $e . '</u>:</td><td><i>' . $f . '</i></td></tr>';
            } else {
$trace .= '<tr><td><b>' . strval($a) . '#</b></td><td align="right"><u>' . $c . '</u>:</td><td></td><td><i>' . $d . '</i></td>';
$trace .= '</table>';
'<br /><br /><br /><font face="Verdana"><center><fieldset style="width: 66%; border: 4px solid white; background: black;"><legend><b>[</b>PHP PDO Error ' . strval($err->getCode()) . '<b>]</b></legend> <table border="0"><tr><td align="right"><b><u>Message:</u></b></td><td><i>' . $err->getMessage() . '</i></td></tr><tr><td align="right"><b><u>Code:</u></b></td><td><i>' . strval($err->getCode()) . '</i></td></tr><tr><td align="right"><b><u>File:</u></b></td><td><i>' . $err->getFile() . '</i></td></tr><tr><td align="right"><b><u>Line:</u></b></td><td><i>' . strval($err->getLine()) . '</i></td></tr><tr><td align="right"><b><u>Trace:</u></b></td><td><br /><br />' . $trace . '</td></tr></table></fieldset></center></font>';

samuelelliot+php dot net at gmail dot com
15.05.2010 9:45
PDOException has two methods for retrieving information about an error. When interpreting the PDOException I run into a problem, the error code that is provided by getCode() is meaningless.  I have come up with a method to make both the error code and message more usable.

A bad username or password would normally provide the following:

CODE : 0
Message : "SQLSTATE[28000] [1045] Access denied for user 'user'@'' (using password: YES)"

Using my extended exception class provides:

CODE: "28000"
Message: "Access denied for user 'user'@'' (using password: YES)"

class pdoDbException extends PDOException {

    public function
__construct(PDOException $e) {
strstr($e->getMessage(), 'SQLSTATE[')) {
preg_match('/SQLSTATE\[(\w+)\] \[(\w+)\] (.*)/', $e->getMessage(), $matches);
$this->code = ($matches[1] == 'HT000' ? $matches[2] : $matches[1]);
$this->message = $matches[3];

To walk threw the method; first the beginning of the message is checked for the SQLSTATE text.  If the text is present, message is then parsed to pull the ANSI code, the SQL specific code, and the message.  The parsed values are stored in there respective variables.  The error code variable stores the ANSI code, unless ANSI is 'HT000' (unmapped error code) then SQL specific code is used.

Using this class is easy; when interacting with PDO use a try catch set of blocks, as follows:

try {
$pdo = new PDO($dns, $username, $password, $options);
} catch (
PDOException $e) {
    throw new

Now you can use the normal error methods to retrieve the real error code and message.

echo $err->getCode(); // Outputs: "28000"
echo $err->getMessage(); // Outputs: "Access denied for user 'user'@'' (using password: YES)"

If you decide to use this code, be aware that the error code is a string (as apposed to PHP standard errors which are integers) as some error codes are alphanumeric.
Typer85 at gmail dot com
23.01.2010 3:13
Here is something interesting regarding a PDOException and it involves some of the annoyances that can be associated with PHP's dynamic nature.

PDOException extends from RuntimeException, which in return extends from Exception. As such, it has access to the $code Protected Class Variable, which represents the Exception's code as an Integer (duh!) and can be accessed externally using the Exception::getCode Method.

Here is the interesting part. PDOException actually redefines $code as a String and not an Integer because for its case, $code actually contains the Exception's SQL State, which is composed of characters and numbers.

It is actually documented in the manual that $code is a String and not an Integer but it might not be immedietley clear because it is hidden by the fact that PDOException::getCode is documented to return an Integer and not a String!

Some developers like to catch a PDOException and rethrow it as a different Exception if they wrap their database calls in an external library. For example, consider the following code:


try {
$PDO = new PDO( '...' ); // PDO Driver DSN. Throws A PDOException.
PDOException $Exception ) {
// PHP Fatal Error. Second Argument Has To Be An Integer, But PDOException::getCode Returns A
    // String.
throw new MyDatabaseException( $Exception->getMessage( ) , $Exception->getCode( ) );


Be careful in that you have to typecast the value returned by PDOException::getCode to an Integer BEFORE you pass it as an Argument to your Exception's Constructor. The following will work:


try {
$PDO = new PDO( '...' ); // PDO Driver DSN. Throws A PDOException.
PDOException $Exception ) {
// Note The Typecast To An Integer!
throw new MyDatabaseException( $Exception->getMessage( ) , (int)$Exception->getCode( ) );


Hope this will save some developers some frustrating hours from an otherwise enjoyable job :)

Good Luck,

PHP Powered Diese Seite bei
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)