- try {
- $DB = new PDO (' Mysql:host=localhost;dbname=test ', $user, $pass);
- $DB = null;
- } catch (Pdoexception $e) {
- Print "Error:". $e->getmessage (). "
";
- Die ();
- }
- ?>
Copy CodeThis 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:
- 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
- }
- ?>
Copy CodeThis exception handling class is a built-in exception handling class that integrates PHP 5. PHP 5 built-in exception handling class structure:
- 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
- }
- ?>
Copy CodeAccordingly, in the code can appropriately call GetFile () and getline () for error location, more convenient debugging. Use process-oriented method code:
- $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;
- ?>
Copy CodeThe 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:
- $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 ();
- ?>
Copy CodeThe 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:
- $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 ());
- ?>
Copy CodeError messages for the output, such as: |