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

Source: Internet
Author: User
The warning mode generates a PHP warning and sets the errorCode attribute. if the warning mode is set, the program will continue to run as follows unless the error code is explicitly checked. Use the warning mode-PDO: ERRMODE_WARNING (Method 2 for capturing errors in SQL statements in PDO)

The warning mode generates a PHP warning and sets the errorCode attribute. if the warning mode is set, the program will continue to run as follows unless the error code is explicitly checked.

In the previous article "use the default mode-PDO: ERRMODE_SILENT (error method 1 captured in SQL statements in PDO, we introduced the first method to capture errors in SQL statements in PDO. next we will introduce the second method: Use the warning mode-PDO: ERRMODE_WARNING ~!

Set the warning mode, read the data in the database through the prepare () and execute () methods, and use the while statement and the fetch () method to output the data cyclically, execute an incorrect SQL statement after setting it to warning mode.

Next I will use an instance to further introduce the warning mode-PDO: ERRMODE_WARNING. I will first introduce the implementation ideas.

Create a php file, connect to the MySQL database, run the SELECT query operation using the prepare () and execute () methods of the pre-processing statement, set an incorrect data table name, and use setAttribute () the method is set to the warning mode. Finally, the data is output cyclically using the while statement and fetch () method. the code is as follows:

 SetAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_WARNING); // Set it to warning mode $ query = "select * from user_12 "; // the SQL statement $ res = $ pdo-> prepare ($ query); // prepare the query statement $ res-> execute (); // execute the query statement, and return the result set?>
Fetch (PDO: FETCH_ASSOC) {// cyclically outputs the query result set and sets the result set as the associated data form. ?> GetMessage ().'') ;}?>
Id User name Password

Note:

In the code above, when defining the SELECT query statement, we intentionally used the wrong data table name user_12 (the correct data table name is: user), which was written for testing!

After the warning mode is set, if an error occurs in the SQL statement, a message is displayed, but the program can continue to run. the result of the above instance is as follows:

Here we will introduce the warning mode. you can try it on your own, so the next article will introduce the method for capturing errors in SQL statements in the last PDO, for details, see "Use exception mode-PDO: ERRMODE_EXCEPTION (method 3 of capturing SQL statements in PDO)".

The above is the details about how to use the warning mode-PDO: ERRMODE_WARNING (Method 2 for capturing SQL statements in PDO). For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.