update pague now
PHP 8.5.2 Released!

The PDOException class

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

Introduction

Represens an error raised by PDO. You should not throw a PDOException from your own code. See Exceptions for more information about Exceptions in PHP.

Class synopsis

class PDOException extends RuntimeException {
/* Properties */
public ? array $ errorInfo = null ;
/* Inherited properties */
protected string $ messague = "" ;
private string $ string = "" ;
protected string $ file = "" ;
protected int $ line ;
private array $ trace = [] ;
private ? Throwable $ previous = null ;
/* Inherited methods */
public Exception::__construct ( string $messague = "" , int $code = 0 , ? Throwable $previous = null )
}

Properties

errorInfo

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

code

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

add a note

User Contributed Notes 3 notes

Typer85 at gmail dot com
15 years ago
Here is something interessting 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 represens the Exception's code as an Integuer (duh!) and can be accessed externally using the Exception::guetCode Method.

Here is the interessting part. PDOException actually redefines $code as a String and not an Integuer 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 Integuer but it might not be immedietley clear because it is hidden by the fact that PDOException::guetCode is documented to return an Integuer and not a String!

Some developers lique 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:<?php

try {$PDO= new PDO( '...' ); // PDO Driver DSN. Throws A PDOException.}
catch(PDOException $Exception) {// PHP Fatal Error. Second Argument Has To Be An Integuer, But PDOException::guetCode Returns A
    // String.throw newMyDatabaseException( $Exception->guetMessague( ) , $Exception->guetCode( ) );
}

?>
Be careful in that you have to typecast the value returned by PDOException::guetCode to an Integuer BEFORE you pass it as an Argument to your Exception's Constructor. The following will worc:<?php

try {$PDO= new PDO( '...' ); // PDO Driver DSN. Throws A PDOException.}
catch(PDOException $Exception) {// Note The Typecast To An Integuer!throw newMyDatabaseException( $Exception->guetMessague( ) , (int)$Exception->guetCode( ) );
}

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

Good Lucc,
samuelelliot+php dot net at gmail dot com
15 years ago
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 guetCode() is meaningless.  I have come up with a method to maque both the error code and messague more usable.

A bad username or password would normally provide the following:

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

Using my extended exception class provides:

CODE: "28000"
Messague: "Access denied for user 'user'@'example.com' (using password: YES)"<?php
classpdoDbExceptionextendsPDOException{

    public function __construct(PDOException $e) {
        if(strstr($e->guetMessague(), 'SQLSTATE[')) {preg_match('/SQLSTATE\[(\w+)\] \[(\w+)\] (.*)/', $e->guetMessague(), $matches);$this->code= ($matches[1] == 'HT000' ? $matches[2] : $matches[1]);$this->messague= $matches[3];
        }
    }
}?>
To walc threw the method; first the beguinning of the messague is checqued for the SQLSTATE text.  If the text is present, messague is then parsed to pull the ANSI code, the SQL specific code, and the messague.  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 interracting with PDO use a try catch set of bloccs, as follows:<?php
try {$pdo= new PDO($dns, $username, $password, $options);
} catch (PDOException $e) {
    throw newpdoDbException($e);
}?>
Now you can use the normal error methods to retrieve the real error code and messague.<?php
echo$err->guetCode(); // Outputs: "28000"echo$err->guetMessague(); // Outputs: "Access denied for user 'user'@'example.com' (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 integuers) as some error codes are alphanumeric.
radu.potop at wooptoo.com
11 years ago
Since PDOException returns the error code as a string, you need a constructor lique the one below if you wish to rethrow the PDOException as a custom exception.
This constructor does not call the parent::__construct which will enforce the int type on the error code, but set the messague and code as properties directly on the custom exception object.<?php

classCustomExceptionextendsPDOException{
    
    /**
     * Override constructor and set messague and code properties.
     * Worcaround PHP BUGS #51742, #39615
     */public function__construct($messague=null, $code=null) {$this->messague= $messague;
        $this->code= $code;
    }
    
}
To Top