Php Catch Database Connection Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow pdo::attr_errmode the company Business Learn more about hiring developers or posting ads with us Stack Overflow pdo error handling Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of php mysqli error 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Catching mysql connection error up vote 0 down vote favorite So i have simple code: try{ $mysqli =
Pdo Insert Query
new mysqli($sql_login['host'], $sql_login['user'], $sql_login['password'] , $sql_login['database'], $sql_login['port'] ); if($mysqli->connect_errno){ throw new Exception("Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error); } } catch (Exception $e) { echo 'Exception: ', $e->getMessage(), "\n"; } the problem is that php returns error and also the exception. Is there something like in java with throw and throws? php oop exception-handling share|improve this question asked Aug 6 '12 at 10:38 user1564141 6111615 php mysql error handling Possible dublicate stackoverflow.com/questions/4862911/… –Dainis Abols Aug 6 '12 at 10:39 ini_set('display_errors', 0); - for better or worse (I'm in the worse camp) errors and exceptions are two completely different things in PHP. –DaveRandom Aug 6 '12 at 10:40 @DainisAbols it isnt mysql error. –user1564141 Aug 6 '12 at 10:41 @DaveRandom i can also use @$mysqli..., but it is not the solution i want. –user1564141 Aug 6 '12 at 10:41 @user1564141 Well unfortunately you have no choice. If something triggers an error, and your error reporting settings are such that it will be displayed, your only option is to suppress it. Like I said, I'm very much in the "worse" camp on this point but you work with what you've got... –DaveRandom Aug 6 '12 at 10:43 | show 3 more comments 3 Answers 3 active oldest votes up vote 0 down vote accepted You can start with installing your own error-handling. One that converts PHP errors into exception. Do it at the beginning of your script. Something likes this: /* |-------------------------------------------------------------------------- | Alternative error handler |-------------------------------------------------------------------------- | | See: http://php.net/manual/en/function.set-error-handler.php | */ function my_error_handler($errno, $errstr, $errfile, $errline) { if (!(error_reporting() & $errno)) { // This error code is not included in error_reporting return; } throw new ErrorExcept
and Objects Namespaces Errors Exceptions Generators References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes pdo close connection Context options and parameters Supported Protocols and Wrappers Security
Pdo::attr_persistent
Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security
Mysqli_report
Filesystem Security Database Security Error Reporting Using Register Globals User Submitted Data Magic Quotes Hiding PHP Keeping Current Features HTTP authentication with PHP http://stackoverflow.com/questions/11826659/catching-mysql-connection-error Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Safe Mode 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 Credit http://php.net/manual/en/pdo.error-handling.php Card Processing 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 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(current page) / Focus search box Large Objects (LOBs) » « Prepared statements and stored procedures PHP Manual Function Reference Database Extensions Abstraction Layers PDO Change language: English Brazilian Portuguese Chinese (Simplified) French Germ
and Objects Namespaces Errors Exceptions Generators References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers Security Introduction http://php.net/manual/en/pdo.connections.php General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting Using Register Globals User Submitted Data Magic Quotes 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 Safe Mode Command error handling 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 Credit Card Processing 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 php catch database 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 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(current page) / Focus search box Transactions and auto-commit » « Predefined Constants PHP Manual Function Reference Database Extensions Abstraction Layers PDO Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Korean Romanian Russian Spanish Turkish Other Edit Report a Bug Connections and Connection management Connections are established by creating instances of the PDO base class. It doesn't matter which driver you want to use; you always use the PDO class name. The constructor accepts parameters for specifying the database source (known as the DSN) and optionally for the username and passwor