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

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

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

ElementMessage
0SQLSTATE Error code (a 5-letter or numeric identifier defined in the ANSI SQL standard).
1Specific driver error code.
2Specific 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:

<?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);//准备查询语句
print_r($pdo->errorInfo());
?>
<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){
        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 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template