- Try {
- $ Db = new pdo ('MySQL: host = localhost; dbname = test', $ user, $ pass );
- $ Db = null;
- } Catch (pdoexception $ e ){
- Print "error:". $ e-> getmessage ()."
";
- Die ();
- }
- ?>
Here, we use the php 5 Object-Oriented exception handling feature. 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:
- 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. Php 5 built-in exception handling class structure:
- 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 the 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 ()! = '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 is executed, errorcode () so we can easily control the display of error messages by ourselves. 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. 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"); // The Table mytable does not exist.
- Echo "errorcode:". $ pdo-> errorcode ();
- ?>
-
Output error code, such: 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. 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"); // The Table mytable does not exist.
- Echo "errorinfo :";
- Print_r ($ pdo-> errorinfo ());
- ?>
Output error information, such: |