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?>
Id |
User name |
Password |
Fetch (PDO: FETCH_ASSOC) {// cyclically outputs the query result set and sets the result set as the associated data form. ?>
|
|
|
GetMessage ().'') ;}?>
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!