Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

how to send an errorlog file and error email

Posted on 2008-06-17
5
Medium Priority
?
304 Views
Last Modified: 2012-06-22
PHP

If example my i cannot connect to database and i want this error message insert into a log file and send as error email notification how?

say i got this

 $dbLink = mysql_connect(ip, username,password);
  if(!$dbLink)   die("Could not connect to database. " . mysql_error());
 
if say the username is incorrect that mean could not connect to database
how can i send this error message to log file and an email?
0
Comment
Question by:firekiller15
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
Khanh Doan earned 1000 total points
ID: 21810074
From PHP Manual :
<?php
// Send notification through the server log if we can not
// connect to the database.
if (!Ora_Logon($username, $password)) {
    error_log("Oracle database not available!", 0);
}
 
// Notify administrator by email if we run out of FOO
if (!($foo = allocate_new_foo())) {
    error_log("Big trouble, we're all out of FOOs!", 1,
               "operator@example.com");
}
 
// another way to call error_log():
error_log("You messed up!", 3, "/var/tmp/my-errors.log");
?>

Open in new window

0
 
LVL 10

Expert Comment

by:Khanh Doan
ID: 21810080
You can check more at http://us.php.net/error_log

goodluck.
Bonmat86.
0
 

Author Comment

by:firekiller15
ID: 21810485
if i want to send log file by email where should i put the extra_headers parameter
0
 

Author Comment

by:firekiller15
ID: 21810565
why no matter it has error or not it will create error file it self
0
 
LVL 10

Expert Comment

by:Khanh Doan
ID: 21810709
I found this script at php manual
I think you can base at it to make ur email report.
Goodluck
Bonmat86.
<?php
 
function myErrorHandler($errno, $errstr, $errfile, $errline){
    switch ($errno) {
    case E_USER_ERROR:
        if ($errstr == "(SQL)"){
            // handling an sql error
            echo "<b>SQL Error</b> [$errno] " . SQLMESSAGE . "<br />\n";
            echo "Query : " . SQLQUERY . "<br />\n";
            echo "On line " . SQLERRORLINE . " in file " . SQLERRORFILE . " ";
            echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
            echo "Aborting...<br />\n";
        } else {
            echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
            echo "  Fatal error on line $errline in file $errfile";
            echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
            echo "Aborting...<br />\n";
        }
        exit(1);
        break;
 
    case E_USER_WARNING:
    case E_USER_NOTICE:
    }
    /* Don't execute PHP internal error handler */
    return true;
}
 
// function to test the error handling
 
function sqlerrorhandler($ERROR, $QUERY, $PHPFILE, $LINE){
    define("SQLQUERY", $QUERY);
    define("SQLMESSAGE", $ERROR);
    define("SQLERRORLINE", $LINE);
    define("SQLERRORFILE", $PHPFILE);
    trigger_error("(SQL)", E_USER_ERROR);
}
 
set_error_handler("myErrorHandler");
 
// trigger an sql error
$query = "SELECT * FROM tbl LIMIT 1";
$sql = @mysql_query($query)
    or sqlerrorhandler("(".mysql_errno().") ".mysql_error(), $query, $_SERVER['PHP_SELF'], __LINE__);
   
 
?>

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

647 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