_php tutorial on the error handling of PDO in PHP

Source: Internet
Author: User
Tags dsn
Object-oriented approach
Let's take a look at the error handling of the PDO in PHP, if the connection error is handled, using an object-oriented approach:
Copy CodeThe code is as follows:
try {
$db = new PDO (' Mysql:host=localhost;dbname=test ', $user, $pass);
$DB = null;
} catch (Pdoexception $e) {
Print "Error:". $e->getmessage (). "
";
Die ();
}
?>

This takes advantage of our 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:
Copy CodeThe code is as follows:
Class Pdoexception extends Exception
{
public $errorInfo = null; Error message, you can call Pdo::errorinfo () or Pdostatement::errorinfo () to access
protected $message; Exception information, you can try Exception::getmessage () to access
protected $code; SQL Status error code, you can use Exception::getcode () to access
}
?>

This exception handling class is integrated with PHP 5 built-in exception handling class, we simply look at PHP 5 built-in exception handling class structure:
Copy CodeThe code is as follows:
Class Exception
{
Property
protected $message = ' Unknown exception '; Exception information
protected $code = 0; User-defined exception codes
protected $file; The file name of the exception that occurred
protected $line; The line number of the code where the exception occurred
Method
Final function getMessage (); Return exception information
Final function GetCode (); Return exception code
Final function getFile (); Returns the file name of the exception that occurred
Final function getLine (); Returns the line number of the code where the exception occurred
Final function gettrace (); BackTrace () array
Final function gettraceasstring (); Gettrace () information that has been rasterized into a string
}
?>

Accordingly, in the code can appropriately call GetFile () and GetLine () for error location, more convenient debugging.
Using a process-oriented approach
Look at the code first:
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 ()! = ' 00000 ') {
Print_r ($db->errorinfo ());
Exit
}
$arr = $rs->fetchall ();
Print_r ($arr);
$DB = null;
?>

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. ErrorInfo () returns an array that includes PHP-defined error codes and MySQL error codes and error messages, with the following array structure:
Array
(
[0] = 42s22
[1] = 1054
[2] = = Unknown column ' aaa ' in ' Field list '
)
After each query execution, 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. The following is an example of using the ErrorCode () method:

"Program 11-17" disc \code\11\pdo\errorcode.php

Copy CodeThe code is as follows:
$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");//table mytable not present
echo "ErrorCode for:". $pdo->errorcode ();
?>

The above code output is shown in error code 11-13.

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 that contains the relevant error information, and the sample code for using the ErrorInfo () method is as follows:

"Program 11-18" disc \code\11\pdo\errorinfo.php
Copy CodeThe code is as follows:
$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");//table mytable not present
echo "ErrorInfo for:";
Print_r ($pdo->errorinfo ());
?>

The above code output is shown in error message 11-14.

http://www.bkjia.com/PHPjc/324303.html www.bkjia.com true http://www.bkjia.com/PHPjc/324303.html techarticle The object-oriented approach is to look at the processing of the connection errors, such as the error handling of PDO in PHP, using an object-oriented approach: Copy the Code as follows:? php try {$db = new PD ...

  • 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.