Error handling methods in PDO

  • Avinash
  • 1
  • Sep 02, 2010
  • Tutorial

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:


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.


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.


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?


$dbh = new PDO( /* your connection string */ );



Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by:

  • demton

    nice post dude.