PDO::errorCode

(no version information, might be only in CVS)

PDO::errorCode --  Fetch the error code associated with the last operation on the database handle

Description

int PDO::errorCode ( void )

Warning

This function is EXPERIMENTAL. The behaviour of this function, the name of this function, and anything else documented about this function may change without notice in a future release of PHP. Use this function at your own risk.

Returns an integer value that maps to the generic error categories defined in the PDO_ERR_* set of constants.

PDO::errorCode() only retrieves error codes for operations performed directly on the database handle. If you create a PDOStatement object through PDO::prepare() or PDO::query() and invoke an error on the statement handle, PDO::errorCode() will return PDO_ERR_NONE. You must call PDOStatement::errorCode() to return the error code for an operation performed on a particular statement handle.

Example 1. Determining which category of error occurred

<?php
/* Provoke an error -- the BONES table does not exist */
$dbh->exec("INSERT INTO bones(skull) VALUES ('reagan')");

echo
"\nPDO::errorCode(): ";
switch (
$dbh->errorCode()) {
    case
PDO_ERR_NONE:
        echo
"No error!\n";
        break;
    case
PDO_ERR_CANT_MAP:
        echo
"Error: Unable to map data types between database and PHP.\n";
        break;
    case
PDO_ERR_SYNTAX:
        echo
"Error: incorrect syntax\n";
        break;
    case
PDO_ERR_CONSTRAINT:
        echo
"Error: The request would violate a constraint.\n";
        break;
    case
PDO_ERR_NOT_FOUND:
        echo
"Error: The object could not be found.\n";
        break;
    case
PDO_ERR_ALREADY_EXISTS:
        echo
"Error: The object already exists.\n";
        break;
    case
PDO_ERR_NOT_IMPLEMENTED:
        echo
"Error: The requested function is not implemented.\n";
        break;
    case
PDO_ERR_MISMATCH:
        echo
"Error: mismatch\n";
        break;
    case
PDO_ERR_TRUNCATED:
        echo
"Error: The value was truncated because the input value was longer
    than the maximum column length.\n"
;
        break;
    case
PDO_ERR_DISCONNECTED:
        echo
"Error: The connection to the requested database has been closed.\n";
        break;
}
?>

The above example will output:

PDO::errorCode(): Error: The object could not be found.