Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to handle errors in PDO in PHP

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how PDO handles errors in PHP. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

In the previous study, we have learned to get the query results through PDO, then we should learn the error handling of PDO. There are two methods to get the error information in the program in PDO, namely the errorCode () method and the errorInfo () method. Next, let's take a look at the application of these two methods.

Before we look at how the errorCode () method and the errorInfo () method perform error handling, let's take a look at the error handling pattern in PDO.

Error handling mode of PDO

There are three different error handling modes available in PDO, which can not only meet different styles of programming, but also adjust the way extensions handle errors. Then let's introduce to you these three different ways to handle errors.

PDO::ERRMODE_SILENT

PDO::ERRMODE_SILENT represents the default mode. When an error occurs in this case, PDO will simply set the error code and will not do anything else. At the same time, you can also use PDO::errorCode () and PDO::errorInfo () to check statements and database objects.

We need to note that if the error occurs because the statement object is called, then you can call the PDOStatement::errorCode () or PDOStatement::errorInfo () method of that object. If the error occurs because of a call to the database object, you can call either PDOStatement::errorCode () or PDOStatement::errorInfo () on the database object.

PDO::ERRMODE_WARNING

The PDO::ERRMODE_WARNING mode sets the error code, and of course, in addition to setting the error code, PDO sends out a message, which is a traditional E_WARNING message. When we need to debug or test, we don't want to interrupt the program but want to figure out what's wrong. It's time for the PDO::ERRMODE_WARNING model to work.

PDO::ERRMODE_EXCEPTION

PDO::ERRMODE_EXCEPTION mode can also set error codes. In addition to setting error codes at first glance, PDO can also throw a PDOException exception class and set its properties to reflect error codes and error messages. PDO::ERRMODE_EXCEPTION mode is also very useful when debugging, it can very quickly identify potential areas in the code that are problematic, because it can effectively magnify the error points in the script.

PDO uses SQL-92 SQLSTATE to standardize error code strings, and different PDO drivers are responsible for mapping their native code to the appropriate SQLSTATE code. The PDO::errorCode () method returns a separate SQLSTATE code.

If you need more details about this error, PDO also provides a PDO::errorInfo () method to return an array containing the SQLSTATE code, the specific driver error code, and the error string for this driver.

Another useful use of exception patterns is that they can build their own error handling more clearly than traditional PHP-style warnings, and require less code / nesting than silent mode and explicitly checking the return value of each database call.

Next, let's take a look at creating an instance of PDO and setting the error mode through an example. Examples are as follows:

In the above example, the error mode is set through the PDO::setAttribute () method. In addition to setting the error mode, you can also set the error mode when creating an PDO instance.

Examples are as follows:

$pdo = new PDO ($dsn, $user, $pwd, array (PDO::ATTR_ERRMODE = > PDO::ERRMODE_WARNING))

These are the three error handling modes of PDO. Let's take a look at the PDO::errorCode () method.

PDO::errorCode () method

The PDO::errorCode () method is mostly used to get error codes that occur when manipulating database handles, which are called SQLSTATE codes.

The PDO::errorCode () method can return a SQLSTATE, a five-letter or numeric identifier defined in the ANSI SQL standard. To put it simply, an SQLSTATE consists of the class values of the first two characters and the subclass values of the next three characters.

Next, let's take a look at connecting to the database through PDO and getting the error code through the errorCode () method.

Examples are as follows:

Output result:

The above result is to get the error code through the errorCode () method. Next let's take a look at the last method, the PDO::errorInfo () method.

PDO::errorInfo () method

The PDO::errorInfo () method, like the PDO::errorCode () method, is used to get the error information that occurs when manipulating the database handle.

The difference is that the return value of the errorInfo () method is an array that contains the relevant error information.

Next, let's take a look at using the query method in PDO to complete the data query operation and get the error message through the errorInfo () method.

Examples are as follows:

In the above example, we query a database that does not exist and output the result:

What we need to note is that both PDO and PDOStatement objects have errorCode () and errorInfo () methods, and errorCode () returns 00000 if there are no errors; otherwise, some error codes are returned. ErrorInfo () returns an array, including the error code defined by PHP and the error code and error message of MySQL.

Thank you for reading! This is the end of the article on "how to handle errors in PDO in PHP". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report