Method of error handling in PDO a-errorcode () method

Source: Internet
Author: User
Tags dsn php foreach
method of error handling in PDO a-errorcode () method

There are two ways to get error messages in a program in PDO: ErrorCode () method and ErrorInfo () Method! Then the next article, we will introduce you to each of these two methods!

Before we introduce you to the three ways to capture the error of the SQL statement in PDO, if you are still not familiar with or do not understand, you can review the use default mode-pdo::errmode_silent (a method of capturing errors in a SQL statement in PDO), Use the warning mode-pdo::errmode_warning (the method of capturing errors in the SQL statement in PDO) and the contents of the three articles using the exception mode-pdo::errmode_exception (the error method in the capture SQL statement in PDO)!

So today we are going to introduce you to the first method of error handling in PDO:

ErrorCode () method

The ErrorCode () method is used to obtain the error code that occurs when manipulating the database handle, which is called the SQLSTATE code, and the syntax of the function is as follows:

INI pdostatement::errorcode (void)

The ErrorCode () method is used to obtain the error code that occurs when manipulating the database handle, which is called the SQLSTATE code, and the syntax of the function is as follows:

INI pdostatement::errorcode (void)

The ErrorCode () method returns a SQLSTATE code that consists of 5 arrays and letters ~

Using the query () method in PDO to complete the querying operation of the data, and through the foreach statement to complete the loop output of the data, use an error data table when defining the SQL statement, and return the error code through the ErrorCode () method, as follows:

Create a PHP file, first connect the MySQL database via PDO, then execute the query through the query () method, then get the error code through the ErrorCode () method, and finally complete the loop output of the data through the foreach statement, the code is as follows:

<?phpheader ("content-type:text/html;    Charset=utf-8 ");                                  Set the encoding format of the page $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"; try{$pdo =new PDO ($DSN, $user, $pwd);//Initialize a PDO object to create a database connection object $pdo $query = "SELECT * from User_12";//SQL statement $res= $pdo->query ($query) to execute,//Prepare query statement $res->execute (); echo "ErrorCode To: ". $pdo->errorcode ()." <br> ";? ><table border= "1" width= "> <tr> <td height=" "align=" center "valign=" Middle ">id</  Td> <td height= "five" align= "center" valign= "Middle" > Username </td> <td height= "" "Align=" center "        valign= "middle" > Password </td> </tr> <?php foreach ($res as $items) {?> <tr>    &LT;TD height= "align=" center "valign=" Middle "><?php echo $items [" id "];? ></td> <td height= "align=" center "valign=" Middle "><?php echo $items [" username "];? ></td> <td height= "align=" center "valign=" Middle "><?php echo $items [" password "];? ></td> </tr> <?php}}catch (Pdoexception $e) {echo "ErrorCode:". $pdo->errorcode ( )."        <br> "; Die ("error!:". $e->getmessage (). '    <br> '); }?></table>

Attention:

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

The results of the operation are as follows:

About the ErrorCode () method Here is the introduction, the next we continue to introduce the second method of error handling in PDO, please read the method of error handling in PDO two-errorinfo () Method!

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.