A detailed description of the PDO error handling method in PHP

Source: Internet
Author: User
Tags dsn getmessage
    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. ?>
Copy Code

This takes advantage of the PHP 5 object-oriented exception handling feature, which initializes the call Pdoexception to initialize an exception class if there is an exception.

Pdoexception the attribute structure of the exception class:

    1. Class Pdoexception extends exception
    2. {
    3. public $errorinfo = null; Error message, you can call Pdo::errorinfo () or Pdostatement::errorinfo () to access
    4. protected $message; Exception information, you can try Exception::getmessage () to access
    5. protected $code; SQL Status error code, you can use Exception::getcode () to access
    6. }
    7. ?>
Copy Code

This exception handling class is a built-in exception handling class that integrates PHP 5. PHP 5 built-in exception handling class structure:

    1. Class exception
    2. {
    3. Property
    4. protected $message = ' Unknown exception '; Exception information
    5. protected $code = 0; User-defined exception codes
    6. protected $file; The file name of the exception that occurred
    7. protected $line; The line number of the code where the exception occurred
    8. Method
    9. Final function getmessage (); Return exception information
    10. Final function GetCode (); Return exception code
    11. Final function getfile (); Returns the file name of the exception that occurred
    12. Final function getline (); Returns the line number of the code where the exception occurred
    13. Final function gettrace (); BackTrace () array
    14. Final function gettraceasstring (); Gettrace () information that has been rasterized into a string
    15. }
    16. ?>
Copy Code

Accordingly, in the code can appropriately call GetFile () and getline () for error location, more convenient debugging. Use 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 ()! = ' 00000 ') {
    4. Print_r ($db->errorinfo ());
    5. Exit
    6. }
    7. $arr = $rs->fetchall ();
    8. Print_r ($arr);
    9. $DB = null;
    10. ?>
Copy Code

The PDO and Pdostatement objects have the ErrorCode () and ErrorInfo () methods, and if there are no errors, errorcode () returns: 00000, or some error code is returned. An array of errorinfo () returns, including PHP-defined error codes and MySQL error codes and error messages, with the following array structure: Array ([0] = 42s22 [1] = 1054 [2] = unknown Colum N ' AAA ' in ' field list ' each time the query is executed, the results of errorcode () are up-to-date, so we can easily control the display of error messages. When using PDO for that PHP and database development process, what if you encounter errors again? Just do it the way it is.

Error handling for 11.3.4 PDO

PDO provides two ways to get error messages from 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 that occurs when the database handle is manipulated, which is called the SQLState code, and the syntax format for the method is as follows:

The return value of the ErrorCode string (void) ErrorCode () method is a sqlstate,sqlstate code that consists of 5 numbers 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");//table mytable not present
    6. echo "ErrorCode for:". $pdo->errorcode ();
    7. ?>
Copy Code

The error code for the output, such as:

2. ErrorInfo () method

The ErrorInfo () method is used to obtain the error message that occurs when the database handle is manipulated, and the syntax of the method is as follows:

The return value of the errorinfo array errorinfo (void) method is an array containing the associated 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");//table mytable not present
    6. echo "ErrorInfo for:";
    7. Print_r ($pdo->errorinfo ());
    8. ?>
Copy Code

Error messages for the output, such as:

  • Related Article

    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.