Use the default mode-----pdo::errmode_silentThe ErrorCode property of the Pdostatement object is set in the default mode, but no other action is made. For example: Add data to the database through the prepare () and execute () methods, set the Errocode property of the Pdostatement object, manually detect errors in the code, and proceed as follows.
$dbms = ' mysql ';//database type $dbname= ' admin ';//Database used $user= ' root ';//database connection user name $pwd= ' password ';//database connection password $host= ' localhost ' ;//Database hostname $dsn= "$dbms: host= $host;p ort=3306;dbname= $dbName"; $pdo =new PDO ($DSN, $user, $pwd);//Initialize a PDO object, is to create a database connection object $pdo$query= INSERT into user (Username,password) VALUES (' admin ');//SQL statement to execute $res= $pdo->prepare ($ query), $res->execute (), $code = $res->errorcode (); Echo $code. ' <br> '; if ($code ==00000) {//If there are no errors, ErrorCode () returns: 00000, otherwise it will return some error code echo "data added successfully";} Else{echo "Database error:<br>"; Echo ' SQL Query: '. $query; Echo ' <pre> '; Var_dump ($res->errorinfo ()); Echo ' < Pre> ';}
The operation results are as follows
21s01
Database error:
SQL Query:insert into User (Username,password) VALUES (' admin ')Array (3) { [0]=> string (5) "21s01" [1]=> Int (1136) [2]=> string (.) "Column count Doesn ' t match value count at row 1 "}
Using warning Mode-----pdo::errmode_warning
Warning mode generates a PHP warning and sets the ErrorCode property. If you set a warning mode, the program will continue to run as it is unless you explicitly check the error code.
For example:
Set the warning mode, read the data in the database through the prepare () and execute () methods, and complete the loop output of the data through the while statement and the fetch () method, and realize the SQL statement that executes the error after setting the warning mode.
$dbms = ' mysql ';//database type $dbname= ' admin ';//Database used $user= ' root ';//database connection user name $pwd= ' password ';//database connection password $host= ' localhost ' ;//Database hostname $dsn= "$dbms: host= $host;p ort=3306;dbname= $dbName"; try {$pdo = new PDO ($DSN, $user, $pwd);//Initialize a PDO object, is to create a database connection object $pdo$pdo->setattribute (Pdo::attr_errmode, pdo::errmode_warning);//set to warning mode $query = "SELECT * FROM Userrr ";//The SQL statement that needs to be executed $res = $pdo->prepare ($query);//Prepare Query statement $res->execute (); while ($result = $res->fetch (PDO: : FETCH_ASSOC) {//while loops out the query result set and sets the result set to be returned as an associative array. echo $result [' ID ']. " " . $result [' username ']. " " . $result [' Password ']; }} catch (Pdoexception $e) {die ("error!:". $e->getmessage (). ' <br> ');} echo "continues to continue to continue to continue";
The results of the operation are as follows:
Warning: D:\wampserver\www\test\test\index1.php
Continue to continue and continue to continue
You can see that after setting the warning mode, if an error in the SQL statement will give a hint, the program will continue to execute.
Using exception mode----pdo::errmode_exception
The exception mode creates a pdoexception and sets the ErrorCode property, which encapsulates the execution code into a try{}catch{} statement block. An uncaught exception will cause the script to break and display a stack trace to let the user know where the problem has occurred.
For example:
Delete information from an erroneous data table
$dbms = ' mysql ';//database type $dbname= ' admin ';//Database used $user= ' root ';//database connection user name $pwd= ' password ';//database connection password $host= ' localhost ' ;//Database hostname $dsn= "$dbms: host= $host;p ort=3306;dbname= $dbName"; try {$pdo = new PDO ($DSN, $user, $pwd);//Initialize a PDO object, is to create a database connection object $pdo$pdo->setattribute (Pdo::attr_errmode, pdo::errmode_exception);//Set exception mode $query = "Delete from Userrr where id=1 ";//SQL statement required to execute $res = $pdo->prepare ($query);//Prepare Query statement $res->execute ();} catch (Pdoexception $e) {echo ' PDO Exception caught: '; Echo ' Error with the database:<br>; Echo ' SQL Query: '. $query; E Cho ' <pre> '; echo "ERROR:". $e->getmessage (). ' <br> '; Echo ' Code: ". $e->getcode (). ' <br> '; Echo ' File: '. $e->getfile (). ' <br> '; echo "line:". $e->getline (). ' <br> '; Echo ' Trace: ". $e->gettraceasstring (). ' <br> '; Echo ' </pre> ';}
Operation Result:
PDO Exception Caught:error with the database:
SQL query:delete from Userrr where id=1ERROR:SQLSTATE[42S02]: Base table or view not found:1146 table ' admin.userrr ' doesn ' t exist
Code:42s02
file:d:\wampserver\www\test\test\index1.php
Line:14
Trace: #0 D:\wampserver\www\test\test\index1.php: Pdostatement->execute () #1 {main}
Capturing errors in the SQL statement in PDO