Object-oriented approach
First look at the handling of connection errors, PDO error handling in PHP, using an object-oriented approach:
Copy Code code 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 our PHP 5 object-oriented exception handling feature to initialize an exception class if there is an exception in the initialization call pdoexception.
Pdoexception the attribute structure of the exception class:
Copy Code code as follows:
<?php
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 the integrated PHP 5 built-in exception handling class, we simply look at the PHP 5 built-in exception handling class structure:
Copy Code code as follows:
<?php
Class Exception
{
Property
protected $message = ' Unknown exception '; Exception information
protected $code = 0; User-defined exception codes
protected $file; Name of the exception that occurred
protected $line; 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 where the exception 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 formatted as a string
}
?>
Accordingly, in the code can be appropriate to call GetFile () and getline () for error location, more convenient for debugging.
Using a process-oriented approach
First look at the code:
Copy Code code 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 errorcode () and ErrorInfo () methods, and if there are no errors, errorcode () returns 00000, or it returns some error codes. ErrorInfo () returns an array that includes PHP-defined error codes and MySQL error codes and error messages, and the array structure is as follows:
Array
(
[0] => 42s22
[1] => 1054
[2] => Unknown column ' aaa ' in ' Field list '
)
The results of errorcode () are up to date every time the query is executed, so we can easily control the error message display.
In the process of using PDO for PHP and database development, what if you encounter any errors? Deal with it the way it is.
error handling of 11.3.4 PDO
PDO provides two methods for obtaining error messages in the program, one is the ErrorCode () method, and the other is the ErrorInfo () method.
1. ErrorCode () method
The ErrorCode () method is used to get the error code that occurs when manipulating the database handle, which is called the SQLState code, and the syntax format of the method is as follows:
The return value of the string errorcode (void) ErrorCode () method is a sqlstate,sqlstate consisting of 5 digits and letters. The following is an example of using the ErrorCode () method:
"Program 11-17" disc \code\11\pdo\errorcode.php
Copy Code code 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");/table MyTable does not exist
echo "ErrorCode as:". $pdo->errorcode ();
?>
The error code for the above code output is shown in Figure 11-13.
2. ErrorInfo () method
The ErrorInfo () method is used to obtain an error message that occurs when manipulating a database handle, and the syntax format of the method is as follows:
The return value of the errorinfo (void) ErrorInfo () method is an array that contains the associated error information, and the example code for the ErrorInfo () method is as follows:
"Program 11-18" disc \code\11\pdo\errorinfo.php
Copy Code code 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");/table MyTable does not exist
echo "errorinfo as:";
Print_r ($pdo->errorinfo ());
?>
The error message for the above code output is shown in Figure 11-14.