(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.1.0)
PDO::errorCode — 獲取跟數(shù)據(jù)庫(kù)句柄上一次操作相關(guān)的 SQLSTATE
返回一個(gè) SQLSTATE,一個(gè)由5個(gè)字母或數(shù)字組成的在 ANSI SQL 標(biāo)準(zhǔn)中定義的標(biāo)識(shí)符。 簡(jiǎn)要地說(shuō),一個(gè) SQLSTATE 由前面兩個(gè)字符的類值和后面三個(gè)字符的子類值組成。 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ù)庫(kù)句柄沒(méi)有進(jìn)行操作,則返回 null
。
示例 #1 取得一個(gè) SQLSTATE 碼
<?php
/* 引發(fā)一個(gè)錯(cuò)誤 -- BONES 數(shù)據(jù)表不存在 */
$dbh->exec("INSERT INTO bones(skull) VALUES ('lucy')");
echo "\nPDO::errorCode(): ";
print $dbh->errorCode();
?>
以上例程會(huì)輸出:
PDO::errorCode(): 42S02