Jump to content

[SOLVED] capturing php errors


lonewolf217

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.");
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.