Detailed explanation of pdo error handling methods in php

Source: Internet
Author: User
Detailed explanation of pdo error handling methods in php

  1. Try {
  2. $ Db = new pdo ('MySQL: host = localhost; dbname = test', $ user, $ pass );
  3. $ Db = null;
  4. } Catch (pdoexception $ e ){
  5. Print "error:". $ e-> getmessage ()."
    ";
  6. Die ();
  7. }
  8. ?>

Here, we use the php 5 Object-Oriented exception handling feature. if there is an exception in it, we will initialize and call pdoexception to initialize an exception class.

The property structure of the pdoexception class:

  1. Class pdoexception extends exception
  2. {
  3. Public $ errorinfo = null; // error message, which can be accessed by calling pdo: errorinfo () or pdostatement: errorinfo ()
  4. Protected $ message; // exception information. you can try exception: getmessage () to access
  5. Protected $ code; // SQL status error code, which can be accessed using exception: getcode ()
  6. }
  7. ?>

This exception handling class is integrated with the php 5 built-in exception handling class. Php 5 built-in exception handling class structure:

  1. Class exception
  2. {
  3. // Attributes
  4. Protected $ message = 'unknown exception'; // exception information
  5. Protected $ code = 0; // custom exception code
  6. Protected $ file; // file name with an exception
  7. Protected $ line; // The code line number with an exception
  8. // Method
  9. Final function getmessage (); // returns exception information
  10. Final function getcode (); // returns the exception code
  11. Final function getfile (); // returns the file name with an exception
  12. Final function getline (); // return the code line number in which an exception occurs.
  13. Final function gettrace (); // backtrace () array
  14. Final function gettraceasstring (); // gettrace () information formatted as a string
  15. }
  16. ?>

Correspondingly, you can call getfile () and getline () in the code to locate the error, making debugging easier. Use the process-oriented method code:

  1. $ Db = new pdo ('MySQL: host = localhost; dbname = test', $ user, $ pass );
  2. $ Rs = $ db-> query ("select aa, bb, cc from foo ");
  3. If ($ db-> errorcode ()! = '000000 '){
  4. Print_r ($ db-> errorinfo ());
  5. Exit;
  6. }
  7. $ Arr = $ rs-> fetchall ();
  8. Print_r ($ arr );
  9. $ Db = null;
  10. ?>

The pdo and pdostatement objects have the errorcode () and errorinfo () methods. if there are no errors, errorcode () returns: 00000. otherwise, some error codes are returned. An array returned by errorinfo (), including the php-defined error code and mysql error code and error information. the array structure is as follows: array ([0] => 42s22 [1] => 1054 [2] => unknown column 'AAA' in 'Field list') after each query is executed, errorcode () so we can easily control the display of error messages by ourselves. What should I do if I encounter another error when I use pdo for php and database development? Follow the above steps.

11.3.4 pdo error handling

Pdo provides two methods to get the error information in the program. one is the errorcode () method, and the other is the errorinfo () method.

1. errorcode () method

The errorcode () method is used to obtain the error code generated when operating the database handle. these error codes are called sqlstate codes. the syntax format of this method is as follows:

01 the return value of the string errorcode (void) errorcode () method is a sqlstate, which consists of five digits and letters.

Example of using the errorcode () method:

  1. $ Dsn = 'MySQL: dbname = shop; host = localhost ';
  2. $ User_name = 'root ';
  3. $ User_psw = 'root ';
  4. $ Pdo = new pdo ($ dsn, $ user_name, $ user_psw );
  5. $ Pdo-> exec ("update mytable set age = 28 where id = 1"); // The Table mytable does not exist.
  6. Echo "errorcode:". $ pdo-> errorcode ();
  7. ?>

Output error code, such:

2. errorinfo () method

The errorinfo () method is used to obtain the error information generated when operating the database handle. the syntax format of this method is as follows:

01 the returned value of the array errorinfo (void) errorinfo () method is an array containing error information.

Use the errorinfo () method:

  1. $ Dsn = 'MySQL: dbname = shop; host = localhost ';
  2. $ User_name = 'root ';
  3. $ User_psw = 'root ';
  4. $ Pdo = new pdo ($ dsn, $ user_name, $ user_psw );
  5. $ Pdo-> exec ("update mytable set age = 28 where id = 1"); // The Table mytable does not exist.
  6. Echo "errorinfo :";
  7. Print_r ($ pdo-> errorinfo ());
  8. ?>

Output error information, such:

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.