Home > Backend Development > PHP Tutorial > Method 1 of error handling in PDO - errorCode() method

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

黄舟
Release: 2023-03-07 17:34:02
Original
3864 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:

<?php
header("Content-Type:text/html; charset=utf-8");    //设置页面的编码格式
$dbms = "mysql";                                  // 数据库的类型
$dbName ="php_cn";                                //使用的数据库名称
$user = "root";                                   //使用的数据库用户名
$pwd = "root";                                    //使用的数据库密码
$host = "localhost";                              //使用的主机名称
$dsn  = "$dbms:host=$host;dbname=$dbName";
try{
$pdo=new PDO($dsn,$user,$pwd);//初始化一个PDO对象,就是创建了数据库连接对象$pdo
$query="select * from user_12";//需要执行的sql语句
$res=$pdo->query($query);//准备查询语句
$res->execute();
echo "errorCode 为:".$pdo->errorCode()."<br>";
?>
<table border="1" width="500">
    <tr>
        <td height="22" align="center" valign="middle">id</td>
        <td height="22" align="center" valign="middle">用户名</td>
        <td height="22" align="center" valign="middle">密码</td>
    </tr>
    <?php
    foreach ($res as $items){
        ?>
        <tr>
            <td height="22" align="center" valign="middle"><?php echo $items["id"];?></td>
            <td height="22" align="center" valign="middle"><?php echo $items["username"];?></td>
            <td height="22" align="center" valign="middle"><?php echo $items["password"];?></td>
        </tr>
        <?php
    }
    }catch(PDOException $e){
echo "errorCode 为:".$pdo->errorCode()."<br>";
        die("Error!:".$e->getMessage().&#39;<br>&#39;);
    }
    ?>
</table>
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template