PDO::errorInfo
(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.1.0)
PDO::errorInfo — Fetch extended error information associated with the last operation on the database handle
Description
Parameters
This function has no parameters.
Return Values
PDO::errorInfo() returns an array of error information about the last operation performed by this database handle. The array consists of at least the following fields:
Element | Information |
---|---|
SQLSTATE error code (a five characters alphanumeric identifier defined in the ANSI SQL standard). | |
1 | Driver-specific error code. |
2 | Driver-specific error message. |
If the SQLSTATE error code is not set or there is no driver-specific error, the elements following element 0 will be set to null .
PDO::errorInfo() only retrieves error information 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::errorInfo() will not reflect the error from the statement handle. You must call PDOStatement::errorInfo() to return the error information for an operation performed on a particular statement handle.
Examples
Example #1 Displaying errorInfo() fields for a PDO_ODBC connection to a DB2 database
The above example will output:
See Also
- PDO::errorCode() — Fetch the SQLSTATE associated with the last operation on the database handle
- PDOStatement::errorCode() — Fetch the SQLSTATE associated with the last operation on the statement handle
- PDOStatement::errorInfo() — Fetch extended error information associated with the last operation on the statement handle
User Contributed Notes 3 notes
Please note : that this example won’t work if PDO::ATTR_EMULATE_PREPARES is true.
You should set it to false
-> setAttribute ( PDO :: ATTR_EMULATE_PREPARES , false );
$stmt = $dbh -> prepare ( ‘bogus sql’ );
if (! $stmt ) <
echo «\nPDO::errorInfo():\n» ;
print_r ( $dbh -> errorInfo ());
>
?>
here are the error codes for sqlite, straight from their site:
The error codes for SQLite version 3 are unchanged from version 2. They are as follows:
#define SQLITE_OK 0 /* Successful result */
#define SQLITE_ERROR 1 /* SQL error or missing database */
#define SQLITE_INTERNAL 2 /* An internal logic error in SQLite */
#define SQLITE_PERM 3 /* Access permission denied */
#define SQLITE_ABORT 4 /* Callback routine requested an abort */
#define SQLITE_BUSY 5 /* The database file is locked */
#define SQLITE_LOCKED 6 /* A table in the database is locked */
#define SQLITE_NOMEM 7 /* A malloc() failed */
#define SQLITE_READONLY 8 /* Attempt to write a readonly database */
#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite_interrupt() */
#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
#define SQLITE_NOTFOUND 12 /* (Internal Only) Table or record not found */
#define SQLITE_FULL 13 /* Insertion failed because database is full */
#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
#define SQLITE_EMPTY 16 /* (Internal Only) Database table is empty */
#define SQLITE_SCHEMA 17 /* The database schema changed */
#define SQLITE_TOOBIG 18 /* Too much data for one row of a table */
#define SQLITE_CONSTRAINT 19 /* Abort due to contraint violation */
#define SQLITE_MISMATCH 20 /* Data type mismatch */
#define SQLITE_MISUSE 21 /* Library used incorrectly */
#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
#define SQLITE_AUTH 23 /* Authorization denied */
#define SQLITE_ROW 100 /* sqlite_step() has another row ready */
#define SQLITE_DONE 101 /* sqlite_step() has finished executing */
Some PDO drivers return a larger array. For example, the SQL Server driver returns 5 values.
For example:
= $db -> exec ( «DELETE FROM [TableName] WHERE ID between 6 and 17» );
print_r ( $db -> errorInfo ());
?>
Result:
Array
(
[0] => 00000
[1] => 0
[2] => (null) [0] (severity 0) []
[3] => 0
[4] => 0
)
Ошибки и их обработка
PDO предлагает на выбор 3 стратегии обработки ошибок в зависимости от вашего стиля разработки приложений.
PDO::ERRMODE_SILENT
До PHP 8.0.0, это был режим по умолчанию. PDO просто предоставит вам код ошибки, который можно получить методами PDO::errorCode() и PDO::errorInfo() . Эти методы реализованы как в объектах запросов, так и в объектах баз данных. Если ошибка вызвана во время выполнения кода объекта запроса, нужно вызвать метод PDOStatement::errorCode() или PDOStatement::errorInfo() этого объекта. Если ошибка вызова объекта базы данных, нужно вызвать аналогичные методы у этого объекта.
PDO::ERRMODE_WARNING
Помимо установки кода ошибки PDO выдаст обычное E_WARNING сообщение. Это может быть полезно при отладке или тестировании, когда нужно видеть, что произошло, но не нужно прерывать работу приложения.
PDO::ERRMODE_EXCEPTION
Начиная с PHP 8.0.0 является режимом по умолчанию. Помимо задания кода ошибки PDO будет выбрасывать исключение PDOException , свойства которого будут отражать код ошибки и её описание. Этот режим также полезен при отладке, так как сразу известно, где в программе произошла ошибка. Это позволяет быстро локализовать и решить проблему. (Не забывайте, что если исключение является причиной завершения работы скрипта, все активные транзакции будут откачены.)
Режим исключений также полезен, так как даёт возможность структурировать обработку ошибок более тщательно, нежели с обычными предупреждениями PHP, а также с меньшей вложенностью кода, чем в случае работы в тихом режиме с явной проверкой возвращаемых значений при каждом обращении к базе данных.
Подробнее об исключениях в PHP смотрите в разделе Исключения.
PDO стандартизирован для работы со строковыми кодами ошибок SQL-92 SQLSTATE. Отдельные драйверы PDO могут задавать соответствия своих собственных кодов кодам SQLSTATE. Метод PDO::errorCode() возвращает одиночный код SQLSTATE. Если необходима специфичная информация об ошибке, PDO предлагает метод PDO::errorInfo() , который возвращает массив, содержащий код SQLSTATE, код ошибки драйвера, а также строку ошибки драйвера.
Пример #1 Создание PDO объекта и установка режима обработки ошибок
= ‘mysql:dbname=testdb;host=127.0.0.1’ ;
$user = ‘dbuser’ ;
$password = ‘dbpass’ ;
$dbh = new PDO ( $dsn , $user , $password );
$dbh -> setAttribute ( PDO :: ATTR_ERRMODE , PDO :: ERRMODE_EXCEPTION );
// PDO выбросит исключение PDOException (если таблица не существует)
$dbh -> query ( «SELECT wrongcolumn FROM wrongtable» );
?>
Результат выполнения данного примера:
Метод PDO::__construct() будет всегда бросать исключение PDOException , если соединение оборвалось, независимо от установленного значения PDO::ATTR_ERRMODE .
Пример #2 Создание экземпляра класса PDO и установка режима обработки ошибок в конструкторе
= ‘mysql:dbname=test;host=127.0.0.1’ ;
$user = ‘googleguy’ ;
$password = ‘googleguy’ ;
$dbh = new PDO ( $dsn , $user , $password , array( PDO :: ATTR_ERRMODE => PDO :: ERRMODE_WARNING ));
// Следующий запрос приводит к ошибке уровня E_WARNING вместо исключения (когда таблица не существует)
$dbh -> query ( «SELECT wrongcolumn FROM wrongtable» );
?>
PDO::errorCode
(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.1.0)
PDO::errorCode — Возвращает код SQLSTATE результата последней операции с базой данных
Описание
Список параметров
У этой функции нет параметров.
Возвращаемые значения
Возвращает SQLSTATE — пятисимвольный идентификатор, определённый в стандарте ANSI SQL-92. Первые два символа SQLSTATE отвечают за класс ошибки, а следующие три определяют её подкласс. Класс ошибок 01 означает предупреждение, которому сопутствует возвращаемый код SQL_SUCCESS_WITH_INFO. Классы отличные от 01, за исключением ‘IM’, означают ошибки выполнения запросов к базе данных. Класс ‘IM’ свидетельствует об ошибках и предупреждениях, которые вызваны самой реализацией PDO (или, возможно, ODBC, если используется драйвер ODBC). Значение подкласса ‘000’ в любом классе означает, что подкласс для этого SQLSTATE отсутствует.
PDO::errorCode() выдаёт код ошибки только для операций, совершаемых с базой данных напрямую из PDO. Если создать объект PDOStatement методами PDO::prepare() или PDO::query() , и вызвать ошибку его методами, PDO::errorCode() эту ошибку не отобразит. Вам нужно вызвать PDOStatement::errorCode() , чтобы получить код ошибки для операции, выполняемой на определённом объекте PDOStatement.
Возвращает null , если никаких операций над базой данных средствами PDO-объекта не производилось.
Примеры
Пример #1 Получение кода SQLSTATE
/* Спровоцируем ошибку — таблицы BONES не существует */
$dbh -> exec ( «INSERT INTO bones(skull) VALUES (‘lucy’)» );
echo «\nPDO::errorCode(): » , $dbh -> errorCode ();
?>
Результат выполнения данного примера:
Смотрите также
- PDO::errorInfo() — Получает расширенную информацию об ошибке, произошедшей в ходе последнего обращения к базе данных
- PDOStatement::errorCode() — Получает код SQLSTATE, связанный с последней операцией в объекте PDOStatement
- PDOStatement::errorInfo() — Получение расширенной информации об ошибке, произошедшей в результате работы объекта PDOStatement
Класс PDOException
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
Введение
Представляет ошибку, вызванную PDO. Вам не следует выбрасывать исключения PDOException из своего кода. Для дополнительной информации об исключениях в PHP смотрите раздел Исключения.
Обзор классов
Свойства
Код ошибки SQLSTATE . Чтобы его получить, используйте Exception::getCode() .
User Contributed Notes 5 notes
Here is something interesting 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 represents the Exception’s code as an Integer (duh!) and can be accessed externally using the Exception::getCode Method.
Here is the interesting part. PDOException actually redefines $code as a String and not an Integer 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 Integer but it might not be immedietley clear because it is hidden by the fact that PDOException::getCode is documented to return an Integer and not a String!
Some developers like 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:
try <
$PDO = new PDO ( ‘. ‘ ); // PDO Driver DSN. Throws A PDOException.
>
catch( PDOException $Exception ) <
// PHP Fatal Error. Second Argument Has To Be An Integer, But PDOException::getCode Returns A
// String.
throw new MyDatabaseException ( $Exception -> getMessage ( ) , $Exception -> getCode ( ) );
>
?>
Be careful in that you have to typecast the value returned by PDOException::getCode to an Integer BEFORE you pass it as an Argument to your Exception’s Constructor. The following will work:
try <
$PDO = new PDO ( ‘. ‘ ); // PDO Driver DSN. Throws A PDOException.
>
catch( PDOException $Exception ) <
// Note The Typecast To An Integer!
throw new MyDatabaseException ( $Exception -> getMessage ( ) , (int) $Exception -> getCode ( ) );
>
?>
Hope this will save some developers some frustrating hours from an otherwise enjoyable job 🙂
Since PDOException returns the error code as a string, you need a constructor like 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 message and code as properties directly on the custom exception object.
class CustomException extends PDOException <
/**
* Override constructor and set message and code properties.
* Workaround PHP BUGS #51742, #39615
*/
public function __construct ( $message = null , $code = null ) <
$this -> message = $message ;
$this -> code = $code ;
>
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 getCode() is meaningless. I have come up with a method to make both the error code and message more usable.
A bad username or password would normally provide the following:
CODE : 0
Message : «SQLSTATE[28000] [1045] Access denied for user ‘user’@’example.com’ (using password: YES)»
Using my extended exception class provides:
CODE: «28000»
Message: «Access denied for user ‘user’@’example.com’ (using password: YES)»
class pdoDbException extends PDOException <
public function __construct ( PDOException $e ) <
if( strstr ( $e -> getMessage (), ‘SQLSTATE[‘ )) <
preg_match ( ‘/SQLSTATE\[(\w+)\] \[(\w+)\] (.*)/’ , $e -> getMessage (), $matches );
$this -> code = ( $matches [ 1 ] == ‘HT000’ ? $matches [ 2 ] : $matches [ 1 ]);
$this -> message = $matches [ 3 ];
>
>
>
?>
To walk threw the method; first the beginning of the message is checked for the SQLSTATE text. If the text is present, message is then parsed to pull the ANSI code, the SQL specific code, and the message. 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 interacting with PDO use a try catch set of blocks, as follows:
try <
$pdo = new PDO ( $dns , $username , $password , $options );
> catch ( PDOException $e ) <
throw new pdoDbException ( $e );
>
?>
Now you can use the normal error methods to retrieve the real error code and message.
echo $err -> getCode (); // Outputs: «28000»
echo $err -> getMessage (); // 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 integers) as some error codes are alphanumeric.
In response to the 15-May-2010 07:45 note from: samuelelliot+php dot net at gmail dot com
Since the most base class Exception takes three arguments, and the previous exception is not the first, .
Since inherited classes to not implicitly call the parent constructor, .
I recommend changing his constructor to this:
public function __construct(string $message = «», int $code = 0, PDOException $e = null)
<
// in case they call: new MyException($somePDOException);
// instead of following the interface.
//
if (is_subclass_of($message, PDOException))
<
$e = $message;
$code = $e->getCode();
$message = $e->getMessage();
>
// Let PDOException do its normal thing
//
parent::__construct($message, $code, $e);
// Now to correct the code number.
//
$state = $this->getMessage();
if(!strstr($state, ‘SQLSTATE[‘))
$state = $this->getCode();
if(strstr($state, ‘SQLSTATE[‘))
<
preg_match(‘/SQLSTATE\[(\w+)\] \[(\w+)\] (.*)/’, $state, $matches);
$this->code = ($matches[1] == ‘HT000’ ? $matches[2] : $matches[1]);
$this->message = $matches[3];
>
>
the sqlstate is an hex integer (as string)
if you want to wrap pdoexception, use
throw new MyDBException($pdoe->getMessage(), hexdec($pdoe->getCode()), $pdoe);