


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)
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['username']&&$_POST['password']!=""){ $name = $_POST['username']; $password = $_POST['password']; $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 ('$name','$password')";//需要执行的sql语句 $res=$pdo->prepare($query);//准备查询语句 $res->execute(); //执行查询语句,并返回结果集 $code =$res->errorCode(); if(empty($code)){ echo "数据添加成功"; }else{ echo "数据错误:<br>"; echo 'SQL Query:'.$query; echo '<pre class="brush:php;toolbar:false">'; var_dump($res->errorInfo()); echo '
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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is a popular web development language that has been used for a long time. The PDO (PHP Data Object) class integrated in PHP is a common way for us to interact with the database during the development of web applications. However, a problem that some PHP developers often encounter is that when using the PDO class to interact with the database, they receive an error like this: PHPFatalerror:CalltoundefinedmethodPDO::prep

As a popular programming language, PHP is widely used in the field of web development. Among them, PHP's PDO_PGSQL extension is a commonly used PHP extension. It provides an interactive interface with the PostgreSQL database and can realize data transmission and interaction between PHP and PostgreSQL. This article will introduce in detail how to use PHP's PDO_PGSQL extension. 1. What is the PDO_PGSQL extension? PDO_PGSQL is an extension library of PHP, which

PHP and PDO: How to perform batch inserts and updates Introduction: When using PHP to write database-related applications, you often encounter situations where you need to batch insert and update data. The traditional approach is to use loops to perform multiple database operations, but this method is inefficient. PHP's PDO (PHPDataObject) provides a more efficient way to perform batch insert and update operations. This article will introduce how to use PDO to implement batch insert and update operations. 1. Introduction to PDO: PDO is PH

PHP and PDO: How to handle JSON data in databases In modern web development, processing and storing large amounts of data is a very important task. With the popularity of mobile applications and cloud computing, more and more data are stored in databases in JSON (JavaScript Object Notation) format. As a commonly used server-side language, PHP's PDO (PHPDataObject) extension provides a convenient way to process and operate databases. Book

PHP and PDO: How to query and display data in pages When developing web applications, querying and displaying data in pages is a very common requirement. Through paging, we can display a certain amount of data at a time, improving page loading speed and user experience. In PHP, the functions of paging query and display of data can be easily realized using the PHP Data Object (PDO) library. This article will introduce how to use PDO in PHP to query and display data by page, and provide corresponding code examples. 1. Create database and data tables

PHP and PDO: How to perform database backup and restore operations When developing web applications, database backup and restore are very important tasks. As a popular server-side scripting language, PHP provides a wealth of libraries and extensions, among which PDO (PHP Data Objects) is a powerful database access abstraction layer. This article will introduce how to use PHP and PDO to perform database backup and restore operations. Step 1: Connect to the database Before actual operation, we need to establish a connection to the database. Use PDO pair

How to use PDO to connect to the Redis database. Redis is an open source, high-performance, in-memory storage key-value database that is commonly used in cache, queue and other scenarios. In PHP development, using Redis can effectively improve the performance and stability of applications. Through the PDO (PHPDataObjects) extension, we can connect and operate the Redis database more conveniently. This article will introduce how to use PDO to connect to a Redis database, with code examples. Install the Redis extension at the beginning

PDOPDO is an object-oriented database access abstraction layer that provides a unified interface for PHP, allowing you to use the same code to interact with different databases (such as Mysql, postgresql, oracle). PDO hides the complexity of underlying database connections and simplifies database operations. Advantages and Disadvantages Advantages: Unified interface, supports multiple databases, simplifies database operations, reduces development difficulty, provides prepared statements, improves security, supports transaction processing Disadvantages: performance may be slightly lower than native extensions, relies on external libraries, may increase overhead, demo code uses PDO Connect to mysql database: $db=newPDO("mysql:host=localhost;dbnam
