PDO error handling in PHP

Source: Internet
Author: User

Object-Oriented Approach
First, let's take a look at the handling of connection errors and PDO errors in PHP, and use object-oriented methods to handle them:
Copy codeThe Code is as follows:
<? Php
Try {
$ Db = new PDO ('mysql: host = localhost; dbname = test', $ user, $ pass );
$ Db = null;
} Catch (PDOException $ e ){
Print "Error:". $ e-> getMessage (). "<br/> ";
Die ();
}
?>

Here we use the object-oriented Exception Handling feature of PHP 5. 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:
Copy codeThe Code is as follows:
<? Php
Class PDOException extends Exception
{
Public $ errorInfo = null; // error message, which can be accessed by calling PDO: errorInfo () or PDOStatement: errorInfo ()
Protected $ message; // Exception information. You can try Exception: getMessage () to access
Protected $ code; // SQL status error code, which can be accessed using Exception: getCode ()
}
?>

This exception handling class is integrated with the PHP 5 built-in exception handling class. Let's take a look at the PHP 5 built-in exception handling class structure:
Copy codeThe Code is as follows:
<? Php
Class Exception
{
// Attributes
Protected $ message = 'unknown exception'; // exception information
Protected $ code = 0; // custom Exception code
Protected $ file; // file name with an exception
Protected $ line; // The code line number with an exception
// Method
Final function getMessage (); // returns exception information
Final function getCode (); // returns the Exception Code
Final function getFile (); // returns the file name with an exception
Final function getLine (); // return the code line number in which an exception occurs.
Final function getTrace (); // backtrace () array
Final function getTraceAsString (); // getTrace () information formatted as a string
}
?>

Correspondingly, you can call getFile () and getLine () in the code to locate the error, making debugging easier.
Use process-oriented methods
First look at the Code:
Copy codeThe Code is as follows:
<?
$ Db = new PDO ('mysql: host = localhost; dbname = test', $ user, $ pass );
$ Rs = $ db-> query ("SELECT aa, bb, cc FROM foo ");
If ($ db-> errorCode ()! = '000000 '){
Print_r ($ db-> errorInfo ());
Exit;
}
$ Arr = $ rs-> fetchAll ();
Print_r ($ arr );
$ Db = null;
?>

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, the results of errorCode () are the latest, so we can easily control the display of error information.
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. The following is an example of using the errorCode () method:

[Program 11-17] cd \ code \ 11 \ pdo \ errorCode. php

Copy codeThe Code is as follows:
<? Php
$ Dsn = 'mysql: dbname = shop; host = localhost ';
$ User_name = 'root ';
$ User_psw = 'root ';
$ Pdo = new PDO ($ dsn, $ user_name, $ user_psw );
$ Pdo-> exec ("update mytable set age = 28 where id = 1"); // The table mytable does not exist.
Echo "errorCode:". $ pdo-> errorCode ();
?>

The error code output from the above Code is 11-13.

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. The sample code for using the errorInfo () method is as follows:

[Program 11-18] cd \ code \ 11 \ pdo \ errorInfo. php
Copy codeThe Code is as follows:
<? Php
$ Dsn = 'mysql: dbname = shop; host = localhost ';
$ User_name = 'root ';
$ User_psw = 'root ';
$ Pdo = new PDO ($ dsn, $ user_name, $ user_psw );
$ Pdo-> exec ("update mytable set age = 28 where id = 1"); // The table mytable does not exist.
Echo "errorInfo :";
Print_r ($ pdo-> errorInfo ());
?>

The error message output from the above Code is 11-14.

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.