Method of error handling in PDO two-errorinfo () method

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

Pdostatement::errorinfo-gets the extended error message associated with the last statement handle operation (PHP 5 >= 5.1.0, PECL PDO >= 0.1.0)!

In the previous article, "The method of error handling in PDO a-errorcode () method" We introduced the first method of error handling in PDO, you can review in the previous article, and then we will introduce you to the second method of error handling in PDO ~

The ErrorInfo () method is used to obtain an error in the information that occurs when manipulating the database handle, and the syntax of the method is as follows:

Array pdostatement::errorinfo (void)

Pdostatement::errorinfo () returns an array of error messages about the operation of the last statement handle. The array contains the following fields:

Elements Information
0 SQLSTATE error code (a 5-letter or number-defined identifier in the ANSI SQL standard).
1 Specific driver error code.
2 Specific driver error messages.

In the PDO through the query () method to complete the operation of the data, and through the foreach statement to complete the data loop output, when the SQL statement is defined using an error table, and through the ErrorInfo () method to return the error message, the implementation steps are as follows:

Create a PHP file, first connect the MySQL database via PDO, then execute the query statement through the query () method, then obtain the error information through the ErrorInfo () method, and finally complete the loop output of the data through the foreach statement, the implementation 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 Print_r ($pdo->errorinfo ());? ><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) {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 result of the output is as follows:

About the two methods of error handling in PDO here we are all finished, I believe that the small partners in the PDO error handling has a certain understanding, still do not understand the small partners can review, the next article we continue to introduce the transaction of PDO, specifically, please read "PDO in the transaction details"!

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.