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?
Avinash Zala currently working at a leading web development company as a Sr. PHP Web Developer. He is the founder of the Xpert Developer. He has completed B.E. in Information Technology. Born and brought up in Ahmedabad. He loves to code and coding is all about passion for him. Follow him on twitter at Avinash Zala & Xpert Developer.
View all posts by: Avinash