-1

i am searching for the code or script that is helpfull to insert all errors into mysql database.i do not know about it ,even not a little bit.

give me any idea about it.

how can i do this means insert all erros in database and the display them in my php page.

amy kumar
  • 11
  • 2

4 Answers4

2

PHP lets you specify you own error handler - set_error_handler().

function error_handler($errno, $errstr, $errfile, $errline) {
    global $db; //naughty use of global. You could re-create your database connection here instead.

    switch ($errno) {
        case E_NOTICE:
        case E_USER_NOTICE:
            $error = 'Notice';
            break;
        case E_WARNING:
        case E_USER_WARNING:
            $error = 'Warning';
            break;
        case E_ERROR:
        case E_USER_ERROR:
            $error = 'Fatal Error';
            break;
        default:
            $error = 'Unknown';
            break;
    }
    $db->query('INSERT INTO ...'); // create error log here

    return true;
}

// Error Handler
set_error_handler('error_handler');

The parameters used in my custom function above have the same names as the manual:

$errno The first parameter, errno, contains the level of the error raised, as an integer.

$errstr The second parameter, errstr, contains the error message, as a string.

$errfile The third parameter is optional, errfile, which contains the filename that the error was raised in, as a string.

$errline The fourth parameter is optional, errline, which contains the line number the error was raised at, as an integer.

Using these parameters you should be able to determine what the error was, what file it was in, and what line it occured on.

Community
  • 1
  • 1
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129
  • Can this cover warnings and info as additional types? or is it only used for errors? – Tikkes Jan 23 '15 at 11:24
  • thanks for the edit on warnings and notices. I will upvote this answer as it is the clearest and the best way to go about the issue of logging errors. – Tikkes Jan 23 '15 at 11:30
  • 1
    You may limit the callback to handle what you like. The full function signature is `set_error_handler ( callable $error_handler [, int $error_types = E_ALL | E_STRICT ] )`. This means, by default, it is showing all errors and warnings. You may limit what is being saved by altering the second parameter, e.g. `set_error_handler ('error_handler', E_ERROR)` to only log errors. – Richard Parnaby-King Jan 23 '15 at 11:32
  • 1
    There is a sister function called [`set_exception_handler`](http://php.net/manual/en/function.set-exception-handler.php) which does the same thing as above, but specifically for exceptions. – Richard Parnaby-King Jan 23 '15 at 11:33
2

i would use something like this to log custom error, but you can modify it around to store all encountered errors.

class Logger extends PDO{
    private $_message;
    public function __construct(){
        parent::__construct("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password);//make sure these matches your database configs
        return null;
    }

    public function setMessage($message){
        $this->_message = $message;
        return $this;

    }

    public function saveMessage(){

        if (!isset($this->_message)){
            die('Error Message has not been set');
        }
        $sql = 'INSERT INTO erros_table error_message,date_created';
        $sql.= 'VALUES (:error_message,:date_created)';

        $query = $this->prepare($sql);
        $query->execute([':error_message' => $this->_message,':date_created' => date('y-m-d')]);

        return null;
    }
}

//now you will do something like this
//you can use this custom class anywhere 




try{

    //execute any code here
    //and make sure to throw an error upon any code fail
    //its up to you to throw exceptions on your custom code, but i advice you so

}catch(Exception $e){
    $logger = new Logger();


    $logger->setMessage($e->getMessage())->saveMessage();


}
nSams Dev
  • 687
  • 2
  • 8
  • 21
1

You're question it's too generic. The main problem here is how to track errors and it's not clear if you want to track client side errors (javascript), server side errors (PHP) or transactions errors (MySQL). In case you want to track all of these, you can develop you're own error handlers. For example, for javascript I use this snippet of code:

    window.jsErrors = [];
    window.onerror = function(errorMessage, url, lineNumber) {
      err = { "host" : window.location.host, "url" : url,
              "error": errorMessage, "line" : lineNumber,
              "user": "John" };
      window.jsErrors[window.jsErrors.length] = err;
      notify(err);
    } 

The notify funcion makes an ajax request. For PHP, take a look at this. For Java, take a look at Log4J DB appender

Community
  • 1
  • 1
Chris
  • 1,140
  • 15
  • 30
0

You should have to do like this for error hanling in mysql.

function throw_ex($er){  
  throw new Exception($er);  
}  
try {  
$q = mysql_query("Insert into test (col1,col2,col3) VALUES ('blah','blah','blah')") or throw_ex(mysql_error());  
}  
catch(exception $e) {
  echo "ex: ".$e; 
}
Nikul
  • 1,025
  • 1
  • 13
  • 33