Tutorial

Error handling methods in PDO

PDO offers us a different type of error handling methods to fit in the style of our application.

There are 3 types of error handling methods in PDO:

PDO::ERRMODE_SILENT

This is the default mode of the error handling in PDO. In this mode PDO will simply set the error code for us to inspect using PDO::errorCode() and PDO::errorInfo() methods on both the statement object and database object. If the error resulted from a call on a statement object then we can use PDOStatement::errorCode() or PDOStatement::errorInfo() on that object but if the error occurred from call on database object then we need to use previous described method to get error details.

PDO::ERRMODE_WARNING

In addition to set this error method, PDO will invoke the traditional E_WARNING message. This setting is useful while testing or debugging the system, if you just want to know what problems occurred without interrupting the flow of the system.

PDO::ERRMODE_EXCEPTION

In addition to setting this error code, PDO will throw  PDOException and set its properties to reflect the error code and error information. This mode is also useful for debugging/testing the system. If error occurs in this mode then it will stop the execution and throw exception and shows the error information. This mode is useful for quickly point to the error place.

(Note: Transactions are automatically rolled back if the exception causes the script to terminate)

PDO standardizes on using SQL-92 SQLSTATE error code strings; individual PDO drivers are responsible for mapping their native codes to the appropriate SQLSTATE codes. The PDO::errorCode() method returns a single SQLSTATE code. If you need more specific information about an error, PDO also offers an PDO::errorInfo() method which returns an array containing the SQLSTATE code, the driver specific error code and driver specific error string.

How to set error mode in PDO?

[cc lang=”php”]

setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );

?>

[/cc]

Shares:
  • demton
    demton
    September 11, 2010 at 2:58 pm

    nice post dude.
    Thanks

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *