Log PHP errors in database

I want to send my PHP errors to a MySQL database and then take the user to a generic error.html page. Is this possible?
LVL 3
sypderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Cornelia YoderArtistCommented:
What php errors?
0
sypderAuthor Commented:
Whatever PHP error occurs (due to a bug). I want to have them recorded in a DB so that I can go back and see what bugs might exist in my code.
0
Aamir SaeedCommented:
bug normally means logical error and to catch logical errors are much more difficult than catching syntax error.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

richdiesalProfessorCommented:
What this really means: major syntax errors likely will not allow your script to run in the first place (an error code would occur), and thus users would never experience them in the first place - which means it's something you would have caught long before users even got access to them.

Logical errors, though, are bugs that don't produce a PHP syntax error but are instead situations where the output you expected is not the output that you received.  This kind of error won't be caught by PHP, as no violation of syntax has occured, and PHP assumes that whatever you did, you meant to do.

If you want to catch PHP warnings or notices, though, that's a different issue.  Although in such cases, I doubt you would want to send users to an error page, as such bugs don't necessarily make your script work improperly.
0
Aamir SaeedCommented:
get latest error by
       http://www.php.net/manual/en/function.error-get-last.php
and insert into DB one by one and then display those to users.
0
sypderAuthor Commented:
I have found that there can be syntax errors as a result of very special cases, and I would just like these recorded. I know that PHP can record these to the log.

I was just wondering if I could get PHP to take someone to error.php if an error occurs and then use the error_get_last command to record it to a DB.
0
Cornelia YoderArtistCommented:
Unfortunately, I don't think you can trap syntax errors right in the php.
0
sypderAuthor Commented:
Thanks yodercm, I figured that might be the case. I would be handing rather than waiting for the client to report an error to me.
0
Aamir SaeedCommented:
you can try something with try/catch, e.g.

<?
try
{
    echo 'aamir';
}
catch( Exception $e )
{
    die($e);
}
?>

if you print
  echo 'aamir'            // without semicolon at the end it 'll not produce error and print aamit

but if you have it in try/catch statement, it 'll give you error
0
Aamir SaeedCommented:
also

The previous error message: $php_errormsg

$php_errormsg is a variable containing the text of the last error message generated by PHP. This variable will only be available within the scope in which the error occurred, and only if the track_errors configuration option is turned on (it defaults to off).
0
Aamir SaeedCommented:
also

Since PHP 4.2.0, PHP returns an appropriate error code along with the file array. The error code can be found in the error segment of the file array that is created during the file upload by PHP. In other words, the error might be found in
   $_FILES['userfile']['error']
0
Mark GilbertSenior Performance EngineerCommented:
Do your own custom error handling by using the following code:

<?php
set_error_handler('myerrors');
?>
What the code above does is tell PHP how you want to handle errors.  By the above we are telling php that if an error is encountered, it should run a function called myerrors.

You can then setup a function that can process that error and log it to your database.  Here's some pseudo code (Heavily edited so as not to infringe on copyright):

<?php
function myerrors($number, $string, $file, $line, $message){

echo "There was an error!";

error_log($error, 3, '/path/to/my/error/log'); // this allows you to log it to your error file.  Change this with your database code to handle it more efficiently
}
?>

Source of highly edited excerpts A Guide to Building Secure Web Applications

Hope this helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sypderAuthor Commented:
Thanks ingwa! That works amazingly well.
0
Mark GilbertSenior Performance EngineerCommented:
Hi Sypder, glad I was able to assist you.  Good luck with your project.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.