how to send an errorlog file and error email

Posted on 2008-06-17
Last Modified: 2012-06-22

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?
Question by:firekiller15
  • 3
  • 2
LVL 10

Accepted Solution

Khanh Doan earned 250 total points
ID: 21810074
From PHP Manual :

// 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,



// another way to call error_log():

error_log("You messed up!", 3, "/var/tmp/my-errors.log");


Open in new window

LVL 10

Expert Comment

by:Khanh Doan
ID: 21810080
You can check more at


Author Comment

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

Author Comment

ID: 21810565
why no matter it has error or not it will create error file it self
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.

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";




    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);


    trigger_error("(SQL)", E_USER_ERROR);



// 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


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Amazon Redshift 2 25
unset shopping cart session 15 29
test if query has no results 2 19
How do I randomize photo images before i move them to the path file? 9 20
Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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.

914 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

15 Experts available now in Live!

Get 1:1 Help Now