PDO::errorCode retrieves the SQLSTATE of the most recent operation on the database handle.

mixed PDO::errorCode();

PDO::errorCode returns a five-char SQLSTATE as a string or NULL if there was no operation on the database handle.

PDO::errorCode in the PDO_SQLSRV driver will return warnings on some successful operations. For example, on a successful connection, PDO::errorCode will return "01000" indicating SQL_SUCCESS_WITH_INFO.

PDO::errorCode only retrieves error codes for operations performed directly on the database connection. If you create a PDOStatement instance through PDO::prepare or PDO::query and generate an error on the statement object, PDO::errorCode will not retrieve that error. You must call PDOStatement::errorCode to return the error code for an operation performed on a particular statement object.

Support for PDO was added in version 2.0 of the Microsoft Drivers for PHP for SQL Server.

In this example, the name of the column is misspelled (Cityx instead of City), causing an error, which is then reported.

$conn = new PDO( "sqlsrv:server=(local) ; Database = AdventureWorks ", "", "");
$query = "SELECT * FROM Person.Address where Cityx = 'Essen'";

print $conn->errorCode();


Other Resources


Community Additions