How do you pass an error message to your custom error handler using trigger_error()? I read the docs and did some searches, and I thought it was pretty straightforward, but I can't seem to get it to work. I have the following code:

//opening connection to db
$connresult = mysql_connect('localhost', $searchdbuser, $searchdbpass);
if (!$connresult)
{
	$problemtext = 'Error opening a db connection in searchlistingsprocess.php.\r\n'.mysql_error($connresult);
	trigger_error($problemtext, E_USER_ERROR);
}

And right now, this line:

$connresult = mysql_connect('localhost', $searchdbuser, $searchdbpass);

generates the following error message:
"mysql_connect(): Access denied for user: 'searchuser@localhost' (Using password: YES)"

And not the error message that I've defined in $problemtext.

I can see that my custom error handler is being called, but the error message that I've defined in $problemtext never gets passed thru to the custom error handler. Here's the code for my custom error handler:

function my_error_handler($error_type, $error_string, $errFile, $errLine)
{
	$userip = $_SERVER['REMOTE_ADDR'];
	$userhostname = $_SERVER['REMOTE_HOST'];
	$subject = 'Unexpected OFRE.com Error: '.date("F j, Y, g:i a");
	$body = "Error Message: $error_string\r\n"."Error Code: $error_type\r\n"."ErrFile: $errFile\r\n"."ErrLine: $errLine\r\n"."User IP Address: $userip\r\n"."User Host Name: $userhostname\r\n"."Variable Dump: $dumpresults\r\n"."========================================================\r\n";
	mail("admin@oursite.com", $subject, $body);
}

I receive an email with the error. But the message that I pass along thru $problemtext never comes thru. Do I even need to include this part:

if (!$connresult)
{
	$problemtext = 'Error opening a db connection in searchlistingsprocess.php.\r\n'.mysql_error($connresult);
	trigger_error($problemtext, E_USER_ERROR);
}

Or if an error is generated by:

$connresult = mysql_connect('localhost', $searchdbuser, $searchdbpass);

Does my customer error handler get called automatically? And how can I get it to pass thru the message that I've defined? Thanks!

    Sorry, but wondering at what point you're calling [man]set_error_handler/man?

      I have a header file that I call at the beginning of all my PHP scripts. Here are the contents of that file:

      function my_error_handler($error_type, $error_string, $errFile, $errLine)
      {
      	$userip = $_SERVER['REMOTE_ADDR'];
      	$userhostname = $_SERVER['REMOTE_HOST'];
      	$subject = 'Unexpected OFRE.com Error: '.date("F j, Y, g:i a");
      	$body = "Error Message: $error_string\r\n"."Error Code: $error_type\r\n"."ErrFile: $errFile\r\n"."ErrLine: $errLine\r\n"."User IP Address: $userip\r\n"."User Host Name: $userhostname\r\n"."Variable Dump: $dumpresults\r\n"."========================================================\r\n";
      	mail("admin@oursite.com", $subject, $body);
      }
      
      set_error_handler('my_error_handler');
      

      Yeah, it's weird. I just can't figure out why my custom error messages are not being passed on along to the custom error handler.

        Try getting rid of all of your 'if(!$connresult)' and simply do this:

        $connresult = @mysql_connect('localhost', 'brad', 'asdf');

        Double check that you've require()'d the header file that loads the function & sets the handler.

        Also, try putting something like "my_error_handler();" right after that query line to try and force the handler to do something. What happens?

          Yup. Double-checked the header file and it's been included with first the function definition and then the call to set_error_handler(). And I just changed my code to:

          $connresult = mysql_connect('localhost', 'fred', 'yabbadabbadoo');
          trigger_error('Error opening a db connection in searchprocess.php.\r\n'.mysql_error($connresult), E_USER_ERROR);
          

          But the error message that gets emailed to me still reads:
          "Error Message: mysql_connect(): Access denied for user: 'fred@localhost'
          (Using password: YES)"

          Instead of:
          "Error Message: Error opening a db connection in searchlistingsprocess.php.
          mysql_connect(): Access denied for user: 'fred@localhost'
          (Using password: YES)"

          It's weird. I can't figure out why it won't pass my message along.

            Write a Reply...