Use exception mode-PDO: ERRMODE_EXCEPTION (error method 3 of capturing SQL statements in PDO)

Source: Internet
Author: User
In exception mode, a PDOException is created and the erorCode attribute is set. it can encapsulate the execution code to a try {...} catch {...} in the statement, uncaptured exceptions will cause the script to be interrupted, and the stack trace will show you the problem! Use exception mode-PDO: ERRMODE_EXCEPTION (error method 3 of capturing SQL statements in PDO)

In exception mode, a PDOException is created and the erorCode attribute is set. it can encapsulate the execution code to a try {...} catch {...} in the statement, uncaptured exceptions will cause the script to be interrupted, and the stack trace will show you the problem!

In the first two articles, use the default mode-PDO: ERRMODE_SILENT (error method 1 captured in SQL statements in PDO), use the warning mode-PDO :: ERRMODE_WARNING (error method 2 for capturing SQL statements in PDO) describes the default mode and exception mode, so today we will introduce the third method to capture SQL statement errors in PDO ~

The exception mode is also very useful. Compared with traditional PHP-style warnings, you can build your own error handling more clearly, and check the return values of each database call compared to the silent mode and explicitly, the exception mode requires less code/nesting.

In addition to setting error codes, PDO also throws a PDOException exception class and sets its attributes to reflect error codes and error messages. This setting is also very useful during debugging, because it will effectively enlarge the points that generate errors in the script, so as to quickly point out potential areas with problems in the code! (Remember: if an exception causes the script to terminate, the transaction is automatically rolled back ).

When deleting data in the database, set it to the exception mode and write an incorrect SQL statement to see the difference between the exception mode and the warning mode and the default mode.

The procedure is as follows:

(1) create a php file, connect to the database, execute the SELECT query using the prepare () and execute () methods of the pre-processing statement, and use the while statement and fetch () method to complete the data loop output, and set the delete hyperlink, connect to another php file, the parameter passed is the data ID value, the code is as follows:

 SetAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // Set it to warning mode $ query = "select * from user "; // 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 Operation
"> Delete

The output result is as follows:

(2) create another file, obtain the data ID value of the hyperlink, connect to the database, set it to exception mode through the setAttribute () method, and define the DELETE statement, delete the data in an incorrect data table and use try {...} catch {...} statement to get error information, the specific code is as follows:

 SetAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // Set it to warning mode $ query = "delete * from user_12 where id = $ id "; // the SQL statement $ res = $ pdo-> prepare ($ query); // prepare the query statement $ res-> bindParam (': ID ', $ _ GET ['code _ id']); // bind to update data $ res-> execute (); // execute the query statement, and return result set} catch (PDOException $ e) {echo "PDO Exception Caught"; echo 'error with the database:
'; Echo' SQL Query; '. $ query; echo'
';    echo "Error:".$e -> getMessage()."
"; echo "Code:".$e ->getCode()."
"; echo "File:".$e ->getFile()."
"; echo "Line:".$e ->getLine()."
"; echo "Trace:".$e ->getTraceAsString()."
"; echo "
";}}

Note:

In the above code, when defining the DELETE 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 exception mode is set, run the wrong SQL statement and the output result is as follows:

The three methods for capturing errors in SQL statements in PDO are described here. I believe our friends have some knowledge about the errors in capturing SQL statements in PDO, in the next article, we will continue to introduce the error handling methods in PDO. for details, please refer to "error handling method 1-errorCode () method in PDO"!

The above describes how to use the exception mode-PDO: ERRMODE_EXCEPTION (error method 3 in the capture SQL statement 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.