Php--pdo Error and error handling

Source: Internet
Author: User
PDO offers three different error-handling modes to suit different styles of application development:

Pdo::errmode_silent This is the default mode. PDO will simply set the error code and use the Pdo::errorcode () and Pdo::errorinfo () methods to examine the statements and database objects. If the error is caused by a call to the statement object, you can call the Pdostatement::errorcode () or Pdostatement::errorinfo () method of that object. If the error occurs because of a call to a database object, you can invoke both methods on the database object.

Pdo::errmode_warning In addition to setting the error code, PDO will emit a traditional e_warning message. This setting is useful during debugging/testing if you just want to see what's going on and don't interrupt the application's process.

Pdo::errmode_exception In addition to setting the error code, PDO throws a Pdoexception exception class and sets its properties to reflect the error code and error message. This setting is also useful during debugging because it effectively magnifies the points in the script that produce the error, so that you can quickly point out potential areas of the code that are problematic (remember: If the exception causes the script to terminate, the transaction is automatically rolled back).

Exception mode Another very useful is that, compared to traditional PHP style warnings, you can build your own error handling more clearly, and the exception pattern requires less code/nesting than silent mode and explicitly checking the return value of each database call.

PDO uses SQL-92 SQLSTATE to standardize the error code strings; The different PDO drivers are responsible for mapping their native code to the appropriate SQLSTATE code. The Pdo::errorcode () method returns a separate SQLSTATE code. If you need more details about this error, PDO also provides a pdo::errorinfo () method to return an array that contains the SQLSTATE code, a specific driver error code, and an error string for this driver.

Example #1 Creating a PDO instance and setting the error mode

<?php    $dsn = ' mysql:dbname=testdb;host=127.0.0.1 ';    $user = ' dbuser ';    $password = ' Dbpass ';    try {        $dbh = new PDO ($DSN, $user, $password);        $DBH->setattribute (Pdo::attr_errmode, pdo::errmode_exception);    } catch (Pdoexception $e) {        echo ' Connection failed: '. $e->getmessage ();    }? >

If Pdo::attr_errmode is currently set, Pdo::__construct () will always throw a pdoexception exception if the connection fails. An uncaught exception is fatal.

Example #2 Create a PDO instance and set the error mode in the constructor

<?php    $dsn = ' mysql:dbname=test;host=127.0.0.1 ';    $user = ' Googleguy ';    $password = ' Googleguy ';    /*     * Use Try/catch around the constructor is still valid, even if Errmode is set to WARNING,     * Because Pdo::__construct will always throw a pdoexception exception if the connection fails.     *    /try {        $dbh = new PDO ($DSN, $user, $password,             array (pdo::attr_errmode = pdo::errmode_warning));    } catch (Pdoexception $e) {        echo ' Connection failed: '. $e->getmessage ();        Exit;    } This will cause the PDO to throw an e_warning level error instead of an exception (when the data table does not exist) $DBH->query ("Select Wrongcolumn from wrongtable"); >

The above routines will output:

Warning:pdo::query (): SQLSTATE[42S02]: Base table or view not found:1146 table ' test.wrongtable ' doesn ' t exist in/tmp/pd o_test.php on line 18
  • 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.