Method 1 of error handling in PDO - errorCode() method

黄舟
Release: 2023-03-07 17:34:02
Original
3723 people have browsed it

Method 1 of error handling in PDO - errorCode() method

There are two methods in PDO to obtain error information in the program: errorCode() method and errorInfo() method! So in the next article, we will introduce these two methods to you one by one!

Previously we introduced to you three methods for capturing SQL statement errors in PDO. If you are still unfamiliar or don’t understand something, you can review "Use the default mode-PDO: ) ##Use exception mode-PDO::ERRMODE_EXCEPTION (three methods of capturing errors in SQL statements in PDO)》Contents in these three articles!So today we will introduce to you the first method of error handling in PDO:

errorCode() method

The errorCode() method is used to obtain the error code that occurs when operating the database handle. These error codes are called: SQLSTATE codes. The syntax format of this function is as follows:

ini PDOStatement::errorCode(void)
Copy after login
Copy after login
errorCode() method uses To obtain error codes that occur when operating database handles, these error codes are called: SQLSTATE codes. The syntax format of this function is as follows:

ini PDOStatement::errorCode(void)
Copy after login
Copy after login

errorCode() method returns a SQLSTATE code, which is Composed of 5 arrays and letters~

The data query operation is completed through the query() method in PDO, and the loop output of the data is completed through the foreach statement. An incorrect data table is used when defining the SQL statement. , and returns the error code through the errorCode() method. The specific implementation is 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 obtain it through the errorCode() method Error code, and finally complete the loop output of data through the foreach statement. The specific code is as follows:

query($query);//准备查询语句 $res->execute(); echo "errorCode 为:".$pdo->errorCode()."
"; ?> errorCode()."
"; die("Error!:".$e->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 running results are as follows:

This is the end of the introduction to the errorCode() method. We will continue to introduce it to you in the next article. The second method of error handling in PDO, please read "

Error handling method two in PDO - errorInfo() methodMethod 1 of error handling in PDO - errorCode() method" for details!

The above is the detailed content of Method 1 of error handling in PDO - errorCode() 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!