PDO::errorCode

(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.1.0)

PDO::errorCode 獲取跟數(shù)據(jù)庫句柄上一次操作相關(guān)的 SQLSTATE

說明

PDO::errorCode(): mixed

返回值

返回一個 SQLSTATE,一個由5個字母或數(shù)字組成的在 ANSI SQL 標準中定義的標識符。 簡要地說,一個 SQLSTATE 由前面兩個字符的類值和后面三個字符的子類值組成。 class value of 01 indicates a warning and is accompanied by a return code of SQL_SUCCESS_WITH_INFO. Class values other than '01', except for the class 'IM', indicate an error. The class 'IM' is specific to warnings and errors that derive from the implementation of PDO (or perhaps ODBC, if you're using the ODBC driver) itself. The subclass value '000' in any class indicates that there is no subclass for that SQLSTATE.

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 not reflect that error. You must call PDOStatement::errorCode() to return the error code for an operation performed on a particular statement handle.

如果數(shù)據(jù)庫句柄沒有進行操作,則返回 null 。

范例

示例 #1 取得一個 SQLSTATE 碼

<?php
/* 引發(fā)一個錯誤 -- BONES 數(shù)據(jù)表不存在 */
$dbh->exec("INSERT INTO bones(skull) VALUES ('lucy')");

echo 
"\nPDO::errorCode(): ";
print 
$dbh->errorCode();
?>

以上例程會輸出:

PDO::errorCode(): 42S02

參見