Home  >  Article  >  Backend Development  >  Use warning mode-PDO::ERRMODE_WARNING (method 2 of capturing errors in SQL statements in PDO)

Use warning mode-PDO::ERRMODE_WARNING (method 2 of capturing errors in SQL statements in PDO)

黄舟
黄舟Original
2017-04-28 17:42:072632browse

Use warning mode -PDO::ERRMODE_WARNING (Method 2 for capturing errors in SQL statements in PDO)

Warning mode will generate a PHP warning, and Set the errorCode attribute. If warning mode is set, the program will continue to run as it does unless the error code is explicitly checked.

Then in our previous article "Use the default mode-PDO::ERRMODE_SILENT (Method 1 to capture errors in SQL statements in PDO)", we introduced the first PDO method to capture errors in SQL statements, then in this article we will introduce the second method: using warning mode-PDO::ERRMODE_WARNING~!

Set the warning mode, read the data in the database through the prepare() and execute() methods, and complete the loop output of the data through the while statement and the fetch() method. The experience is executed after setting the warning mode. Wrong SQL statement.

Below I will use examples to further introduce the use of warning mode-PDO::ERRMODE_WARNING. First, let me introduce the idea of ​​​​implementation.

First create a php file, connect to the MySQL database, execute the SELECT query operation through the prepare() and execute() methods of the prepared statement, and set an incorrect data table name, and set it to through the setAttribute() method. Warning mode, finally complete the loop output of data through while statement and fetch() method. 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
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);  //设置为警告模式
$query="select * from user_12";//需要执行的sql语句
$res=$pdo->prepare($query);//准备查询语句
$res->execute();            //执行查询语句,并返回结果集
?>
<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
    while($result=$res->fetch(PDO::FETCH_ASSOC)){         // 循环输出查询结果集,并且设置结果集为关联数据形式。
        ?>
        <tr>
            <td height="22" align="center" valign="middle"><?php echo $result["id"];?></td>
            <td height="22" align="center" valign="middle"><?php echo $result["username"];?></td>
            <td height="22" align="center" valign="middle"><?php echo $result["password"];?></td>
        </tr>
        <?php
    }
    }catch(Exception $e){
        die("Error!:".$e->getMessage().&#39;<br>&#39;);
    }
    ?>
</table>

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!

After setting to warning mode, if an error occurs in the SQL statement, a prompt message will be given, but the program can still continue to execute. The results obtained by the above example are as follows:

Use warning mode-PDO::ERRMODE_WARNING (method 2 of capturing errors in SQL statements in PDO)

That’s all about the warning mode. You can try it locally. Then our next article will introduce you to the last SQL statement captured in PDO. For error methods, please read "Using Exception Mode-PDO::ERRMODE_EXCEPTION (Capturing Error Methods in SQL Statements in PDO Three)"!

The above is the detailed content of Use warning mode-PDO::ERRMODE_WARNING (method 2 of capturing errors in SQL statements in PDO). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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