Use the default mode-pdo::errmode_silent (the error method in the capture SQL statement in PDO one)

Source: Internet
Author: User
Use the default mode-pdo::errmode_silent (the error method in the capture SQL statement in PDO one)

Capturing SQL statement errors in PDO there are three options to choose from, according to your own development projects and the actual situation to choose the appropriate scenario to capture the SQL statement error!

So we've covered three ways to execute SQL statements in PDO in the previous article, three ways to execute SQL statements in PDO, and a detailed approach to fetching result sets from the previous three articles "PDO" () Fetchall () Three ways to get result sets in PDO are described in the Fetchcolumn () method of obtaining result sets in PDO, so let's introduce some ways to capture the errors in the SQL statement in PDO today!

Today we introduce the default mode-pdo::errmode_silent.

Sets the ErrorCode data for the Pdostatement object in the default mode, but does nothing else.

Below we use the example to introduce the default mode-pdo::errmode_silent specific use, the specific steps are as follows:

Start by creating a PHP file, adding a form, submitting the form element to this page, connecting the MySQL database via PDO, performing an INSERT add operation through the prepare () and execute () methods of the preprocessing statement, adding data to the data table, and setting Pdostatement the ErrorCode property of the object to detect errors in the code as follows:

<form action= "1.php" Name= "Form1" method= "post" > User name: <input type= "text" name= "username" > Password: <input type= "password" name= "password" > <input type= "Submit" name= "submit" value= "Submit" ></form><?phphea Der ("Content-type:text/html;    Charset=utf-8 "); Set the encoding format for the page if ($_post[' username ']&&$_post[' password ']!= "") {$name = $_post[' username ']; $password = $_post['                                  Password ']; $dbms = "MySQL";                                The type of database $dbname = "PHP_CN";                                   The database name used is $user = "root";                                    Database user name used $pwd = "root";                              The database password used $host = "localhost";    Host name used $DSN = "$dbms: host= $host;d bname= $dbName"; $pdo =new PDO ($DSN, $user, $pwd);//Initialize a PDO object to create a database connection object $pdo $query = "INSERT INTO ' User_12 ' (Username,password) VALUES            (' $name ', ' $password ');//SQL statement to execute $res = $pdo->prepare ($query);//Prepare query Statement $res->execute (); Execute Query Languageand returns the result set $code = $res->errorcode ();    if (empty ($code)) {echo "data added successfully";        }else{echo "Data error:<br>";        Echo ' SQL Query: '. $query;        Echo ' <pre> ';        Var_dump ($res->errorinfo ());    Echo ' </pre> '; }}?>

Attention:

In the above code, when defining INSERT add statement, we deliberately used the wrong data table name User_12 (the correct data table name is: User), here is to test write!

The resulting error output is as follows:

Then using the default mode-pdoerrmode_silent capture the error method in the SQL statement in PDO, you can try it out, next we'll continue to introduce you to the error method in the capture SQL statement in PDO, please read the Using warning mode-pdo:: Errmode_warning (the error method in the capture SQL statement in PDO II) "!

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.