Home > Backend Development > PHP Tutorial > Use the default mode -PDO::ERRMODE_SILENT (Method 1 to capture errors in SQL statements in PDO)

Use the default mode -PDO::ERRMODE_SILENT (Method 1 to capture errors in SQL statements in PDO)

黄舟
Release: 2023-03-07 17:28:01
Original
2960 people have browsed it

Use the default mode -PDO::ERRMODE_SILENT (Method 1 to capture errors in SQL statements in PDO)

There are three ways to capture SQL statement errors in PDO You can choose a solution. Choose a suitable solution according to your own development project and actual situation to capture SQL statement errors!

Then we introduced the three methods of executing SQL statements in PDO in the previous article "Three ways to execute SQL statements in PDO", and in the previous three articles "Detailed explanation of the fetch() method for obtaining the result set in PDO》《Detailed explanation of the fetchAll() method for obtaining the result set in PDO》《The fetchColumn() method for obtaining the result set in PDO "Detailed Explanation" introduced three methods of obtaining result sets in PDO, so today we will introduce to you several methods of capturing errors in SQL statements in PDO~!

Today I will introduce to you how to use the default mode-PDO::ERRMODE_SILENT.

Set the errorCode data of the PDOStatement object in the default mode, but do not perform any other operations.

Let’s use examples to introduce the specific use of the default mode-PDO::ERRMODE_SILENT. The specific steps are as follows:

First create a php file, add a form, and submit the form elements to this page. , connect to the MySQL database through PDO, perform INSERT addition operations through the prepare() and execute() methods of the prepared statement, add data to the data table, and set the errorCode attribute of the PDOStatement object to detect errors in the code. The specific code is as follows :

<form action="1.php" name="form1" method="post">
    用户名:<input type="text" name="username">
    密码:  <input type="password" name="password">
           <input type="submit" name="Submit" value="提交">
</form>
<?php
header("Content-Type:text/html; charset=utf-8");    //设置页面的编码格式
if($_POST[&#39;username&#39;]&&$_POST[&#39;password&#39;]!=""){
$name = $_POST[&#39;username&#39;];
$password = $_POST[&#39;password&#39;];
$dbms = "mysql";                                  // 数据库的类型
$dbName ="php_cn";                                //使用的数据库名称
$user = "root";                                   //使用的数据库用户名
$pwd = "root";                                    //使用的数据库密码
$host = "localhost";                              //使用的主机名称
$dsn  = "$dbms:host=$host;dbname=$dbName";
    $pdo=new PDO($dsn,$user,$pwd);//初始化一个PDO对象,就是创建了数据库连接对象$pdo
    $query="insert into `user_12`(username,password) VALUES (&#39;$name&#39;,&#39;$password&#39;)";//需要执行的sql语句
    $res=$pdo->prepare($query);//准备查询语句
    $res->execute();            //执行查询语句,并返回结果集
    $code =$res->errorCode();
    if(empty($code)){
        echo "数据添加成功";
    }else{
        echo "数据错误:<br>";
        echo &#39;SQL Query:&#39;.$query;
        echo &#39;<pre class="brush:php;toolbar:false">&#39;;
        var_dump($res->errorInfo());
        echo &#39;
Copy after login
'; } } ?>

Note:

In the above code, when defining the INSERT statement, we deliberately The wrong data table name user_12 is used (the correct data table name is: user), this is written for testing!

The error output result is as follows:

Use the default mode -PDO::ERRMODE_SILENT (Method 1 to capture errors in SQL statements in PDO)

Then use the default mode-PDOERRMODE_SILENT to capture the error method in the SQL statement in PDO. That’s it, you can give it a try. In the next article, we will continue to introduce you to the method of capturing errors in SQL statements in PDO. For details, please read "Use warning mode-PDO::ERRMODE_WARNING(How to capture errors in SQL statements in PDO two)"!

The above is the detailed content of Use the default mode -PDO::ERRMODE_SILENT (Method 1 to capture errors in SQL statements in PDO). 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