Method 2 of error handling in PDO - errorInfo() method

黄舟
Release: 2023-03-07 17:36:02
Original
3836 people have browsed it

Method 2 of error handling in PDO - errorInfo() method

PDOStatement::errorInfo — Obtain extended error information related to the last statement handle operation (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)!

In the previous article "Error handling method in PDO one - errorCode() method" we introduced the first method of error handling in PDO. You can read it in the previous article After reviewing this article, we will introduce the second method of error handling in PDO~

errorInfo() method is used to obtain information errors that occur when operating database handles. The syntax format of this method is as follows :

array PDOStatement::errorInfo(void)
Copy after login

PDOStatement::errorInfo() Returns an array of error information about the last operation performed by the statement handle. The array contains the following fields:

Element Message
0 SQLSTATE Error code (a 5-letter or numeric identifier defined in the ANSI SQL standard).
1 Specific driver error code.
2 Specific driver error information.

Complete the data query operation through the query() method in PDO, and complete the loop output of the data through the foreach statement. Use an incorrect data when defining the SQL statement. table, and return error information through the errorInfo() method. The specific implementation steps are as follows:

Create a php file, first connect to the MySQL database through PDO, then execute the query statement through the query() method, and then through errorInfo() method to obtain error information, and finally complete the loop output of data through the foreach statement. The specific implementation code is as follows:

query($query);//准备查询语句 print_r($pdo->errorInfo()); ?> getMessage().'
'); } ?>
id 用户名 密码
Copy after login

Note:

In the above code, when defining the SELECT query statement, we deliberately used the wrong data table name user_12 (the correct data table name is: user). This is written for testing!

The output result is shown in the figure below:

Method 2 of error handling in PDO - errorInfo() method

We will introduce all the two methods of error handling in PDO here. That's it. I believe that everyone has a certain understanding of error handling in PDO. Those who haven't understood it can review it again. In the next article, we will continue to introduce the transaction processing of PDO to you. For details, please read "PDO Detailed introduction to transaction processing》!

The above is the detailed content of Method 2 of error handling in PDO - errorInfo() method. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!