PDOStatement: errorInfo-get the extension error information related to the last statement handle operation (PHP5 & gt; 5.1.0, PECLpdo & gt; 0.1.0 )!
Method 2 of error handling in PDO-errorInfo ()
PDOStatement: errorInfo-get the extension error information related to the last statement handle operation (PHP 5 >=5.1.0, PECL pdo >=0.1.0 )!
In the previous article "method 1 for error handling in PDO-errorCode () method", we introduced the first method for error handling in PDO. you can review it in the previous article, next, we will introduce the second method for error handling in PDO ~
The error message returned when the errorInfo () method is used to obtain the handle of an operating Database. the syntax format of this method is as follows:
array PDOStatement::errorInfo(void)
PDOStatement: errorInfo () returns an array of error messages about the last statement handle operation. This array contains the following fields:
Element |
Information |
0 |
SQLSTATE error code (an identifier defined in the ansi SQL standard consisting of five letters or numbers ). |
1 |
The specific driver error code. |
2 |
Specific driver error information. |
In PDO, you can use the query () method to query data and use the foreach statement to output data cyclically. when defining an SQL statement, you can use an incorrect data table, the error message is returned using the errorInfo () method. the specific implementation steps are as follows:
Create a php file, connect to the MySQL database through PDO, execute the query statement using the query () method, and then obtain the error information using the errorInfo () method, finally, use the foreach statement to output data cyclically. the specific implementation code is as follows:
Query ($ query); // prepare the query statement print_r ($ pdo-> errorInfo ();?>
Id |
User name |
Password |
|
|
|
GetMessage ().'') ;}?>
Note:
In the code above, when defining the SELECT query statement, we intentionally used the wrong data table name user_12 (the correct data table name is: user), which was written for testing!
The output result is shown in:
The two methods of error handling in PDO are all described here. I believe our friends have some knowledge about the error handling in PDO. if you have not yet understood it, you can review it again, in the next article, we will continue to introduce the transaction processing of PDO. for details, please read the specific introduction to transaction processing in PDO!
The above is the details of the error handling method 2-errorInfo () in PDO. For more information, see other related articles in the first PHP community!