Solved

Log PHP errors in database

Posted on 2007-03-19
14
253 Views
Last Modified: 2013-12-13
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?
0
Comment
Question by:sypder
  • 5
  • 4
  • 2
  • +2
14 Comments
 
LVL 27

Expert Comment

by:yodercm
ID: 18752640
What php errors?
0
 
LVL 3

Author Comment

by:sypder
ID: 18752680
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
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 18752721
bug normally means logical error and to catch logical errors are much more difficult than catching syntax error.
0
 
LVL 9

Expert Comment

by:richdiesal
ID: 18752749
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
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 18752784
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
 
LVL 3

Author Comment

by:sypder
ID: 18752802
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
 
LVL 27

Expert Comment

by:yodercm
ID: 18752928
Unfortunately, I don't think you can trap syntax errors right in the php.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 3

Author Comment

by:sypder
ID: 18752937
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
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 18752991
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
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 18753053
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
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 18753089
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
 
LVL 18

Accepted Solution

by:
ingwa earned 500 total points
ID: 18757788
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
 
LVL 3

Author Comment

by:sypder
ID: 18758523
Thanks ingwa! That works amazingly well.
0
 
LVL 18

Expert Comment

by:ingwa
ID: 18759270
Hi Sypder, glad I was able to assist you.  Good luck with your project.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now