lonewolf217 Posted January 5, 2009 Share Posted January 5, 2009 I have a question regarding how to capture php errors, besides just logging them to a file. Say for example I attempt a mssql_connect() and it fails because the SQL services are down. in my error log I get this: [05-Jan-2009 15:12:41] PHP Warning: mssql_connect() [<a href='function.mssql-connect'>function.mssql-connect</a>]: Unable to connect to server: <SERVER> in <PAGE> on line 27 I am wondering if there is some php variable that also captures this message so I could email it to myself, since mysql_error() doesn't seem to capture it Quote Link to comment Share on other sites More sharing options...
castis Posted January 5, 2009 Share Posted January 5, 2009 for that specific instance right there, do this. try { @$Connection = new Mysqli( $location, $username, $password, $database); if (mysqli_connect_errno()) { throw new Exception("Could not connect to ". $database); } } catch (Exception $e) { exit($e->GetMessage()); } that should disable anything else from happening if the database connection fails. edit: the error suppression operator is expensive to use but theres no other way around that one. Quote Link to comment Share on other sites More sharing options...
premiso Posted January 5, 2009 Share Posted January 5, 2009 Also, if you do not have php 5 or would prefer not to use try/catch (no reason not to imo but yea) set_error_handler PHP has a way for you to define your own way to handle php errors. But as stated, the try/catch is much easier and much less of a chance for an infinite loop =) Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 5, 2009 Share Posted January 5, 2009 they are using mssql...you can use the same concept though...or just: if(!@mssql('server','username','password')){ @mail('youremail@host.com','Site Problem','The MSSQL Server is Down'); die("The site is down for maintenance. Please visit back soon."); } Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted January 5, 2009 Author Share Posted January 5, 2009 Thanks, yes I did understand the basic concept of sending the mail that there is an error but I was just hoping to put the actual error message into the email body rather than just saying "hey, there's an error". If it isn't an easy method to accomplish though, Ill just go the generic email route. Thanks Quote Link to comment Share on other sites More sharing options...
premiso Posted January 5, 2009 Share Posted January 5, 2009 mssql_get_last_message if(!@mssql('server','username','password')){ @mail('youremail@host.com','Site Problem','The MSSQL Server is Down Error:' . mssql_get_last_message()); die("The site is down for maintenance. Please visit back soon."); } I think that would work. Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted January 5, 2009 Author Share Posted January 5, 2009 ive tried that, got nothing Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 5, 2009 Share Posted January 5, 2009 this will work on EVERY problem...not just for mssql: <?php // error handler function function myErrorHandler($errno, $errstr, $errfile, $errline) { //Check to see if we should report on it if($errno && ($errno & error_reporting())){ $msg = sprintf("%s in %s [%s]",$errstr,$errfile,$errline); @mail('youremail@host.com','Site Problem',$msg); echo "The site is down for maintenance. Please visit back soon."; exit; } return true; } // set to the user defined error handler set_error_handler("myErrorHandler"); mssql_connect('testserver'); ?> the top part of the code should go in some include file that gets run at the beginning of every script Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted January 5, 2009 Author Share Posted January 5, 2009 Thanks, this approach looks promising, but I got some errors. does the "set_error_handler" go in the common include page as well? I put it there and I am getting this [05-Jan-2009 15:54:28] PHP Fatal error: Cannot redeclare myerrorhandler() (previously declared in \define.php:36) in \define.php on line 46 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 5, 2009 Share Posted January 5, 2009 this is the block of code that should be included: <?php // error handler function function myErrorHandler($errno, $errstr, $errfile, $errline) { //Check to see if we should report on it if($errno && ($errno & error_reporting())){ $msg = sprintf("%s in %s [%s]",$errstr,$errfile,$errline); @mail('youremail@host.com','Site Problem',$msg); echo "The site is down for maintenance. Please visit back soon."; exit; } return true; } // set to the user defined error handler set_error_handler("myErrorHandler"); ?> use require_once() to include it though to make sure it's not included more then once Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted January 5, 2009 Author Share Posted January 5, 2009 Thanks, I did not know about require_once() and i have no idea why it was being loaded twice. That being said, your code works perfectly! Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.