![]() Since the most base class Exception takes three arguments, and the previous exception is not the first. In response to the 1 07:45 note from: samuelelliot php dot net at gmail dot com If you decide to use this code, be aware that the error code is a string (as apposed to PHP standard errors which are integers) as some error codes are alphanumeric. Now you can use the normal error methods to retrieve the real error code and message.Įcho $err -> getMessage () // Outputs: "Access denied for user (using password: YES)" Using this class is easy when interacting with PDO use a try catch set of blocks, as follows: The error code variable stores the ANSI code, unless ANSI is 'HT000' (unmapped error code) then SQL specific code is used. The parsed values are stored in there respective variables. If the text is present, message is then parsed to pull the ANSI code, the SQL specific code, and the message. To walk threw the method first the beginning of the message is checked for the SQLSTATE text. $this -> code = ( $matches = 'HT000' ? $matches : $matches ) Preg_match ( '/SQLSTATE\ \ (.*)/', $e -> getMessage (), $matches ) Message: "Access denied for user (using password: YES)" Using my extended exception class provides: Message : "SQLSTATE Access denied for user (using password: YES)" I have come up with a method to make both the error code and message more usable.Ī bad username or password would normally provide the following: When interpreting the PDOException I run into a problem, the error code that is provided by getCode() is meaningless. PDOException has two methods for retrieving information about an error. Hope this will save some developers some frustrating hours from an otherwise enjoyable job :) getMessage ( ), (int) $Exception -> getCode ( ) ) getMessage ( ), $Exception -> getCode ( ) ) īe careful in that you have to typecast the value returned by PDOException::getCode to an Integer BEFORE you pass it as an Argument to your Exception's Constructor. For example, consider the following code: Some developers like to catch a PDOException and rethrow it as a different Exception if they wrap their database calls in an external library. It is actually documented in the manual that $code is a String and not an Integer but it might not be immedietley clear because it is hidden by the fact that PDOException::getCode is documented to return an Integer and not a String! PDOException actually redefines $code as a String and not an Integer because for its case, $code actually contains the Exception's SQL State, which is composed of characters and numbers. ![]() As such, it has access to the $code Protected Class Variable, which represents the Exception's code as an Integer (duh!) and can be accessed externally using the Exception::getCode Method. ![]() PDOException extends from RuntimeException, which in return extends from Exception. Here is something interesting regarding a PDOException and it involves some of the annoyances that can be associated with PHP's dynamic nature. ![]() Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes and Objects Namespaces Enumerations Errors Exceptions Fibers Generators Attributes References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Predefined Attributes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting User Submitted Data Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts ? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |