Try...Catch code not working. Can you see why?

Discussion in 'PHP' started by Masterful, Oct 29, 2014.

  1. #1
    Below is how I handle MySQL errors.

    Basically, if the connection fails or if there's a MySQL error, the error is supposed to be logged and I am meant to be sent an e-mail. Obviously, I don't want to receive thousands of e-mails, so the e-mail is supposed to be sent only if it's been over 30 minutes (1,800 seconds) since the previous e-mail was sent. Everything works, except the e-mail, which isn't sent. And I can't work out the reason.

    The two variables ($last_modified and $time_elapsed) are set, but the e-mail just isn't being sent. It's got something to do with the if statement. When I remove it, the code works.

    Can anyone see the problem? Any assistance will be much appreciated.


    function handle_error($err) {
    throw new Exception($err);
    }
    try {
    $con = mysql_connect("", "", "") or handle_error("mysql_connect failed! ");
    mysql_select_db("") or handle_error("mysql_select_db failed! ");
    $sql = "SELECT blah blah blah ...";
    $result = mysql_query($sql, $con) or handle_error("mysql_query failed! ");
    }
    catch(exception $e) {
    // Log error in error-log.txt.
    trigger_error($e->getMessage() . mysql_error(), E_USER_WARNING);
    // Send yourself an email if more than 30 mins (1800 seconds) have passed since previous e-mail.
    $last_modified = filemtime('error-log.txt');
    $time_elapsed = time() - $last_modified;
    if($time_elapsed > 1800) {
    error_log($e->getMessage() . "Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "example@aol.com", "From: example@yahoo.com");
    }
    } 
    PHP:
     
    Masterful, Oct 29, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Not really sure why it doesn't work - I tested just the
    $last_modified, $time_elapsed with a couple different files on the server (one old, and one newly created), and it works just fine (the if triggers if the file is older than 1800 seconds).
     
    PoPSiCLe, Oct 29, 2014 IP
  3. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Thanks.

    I can see the problem now. It's quite stupid of me.

    This is what the script's doing:
    1. Whenever there's an error, log it in error-log.txt.
    2. Check when error-log.txt was last modified (which would always be just a split second ago).
    3. Send e-mail only if $last_modified > 1,800 (which it never is).

    Even if I changed the order so that the e-mail is sent before the error is logged, the script is still flawed because it would not work if errors are constantly occurring. It would send the first e-mail and that's it; it won't send one every 30 minutes.

    The only way around it is to check not when error-log.txt was last modified but instead check when I last sent an e-mail.

    This is my new code—and it works!

    // When there's an error, log it in error file.
    trigger_error($e->getMessage(). mysql_error(), E_USER_WARNING);
    // Also, send self an email if more than 30 minutes have passed since previous error email.
    $LastEmailSent =(int)file_get_contents('last_email_sent.txt');
    $TimeElapsed = time()- $LastEmailSent;
    if($TimeElapsed >1800) {
         error_log($e->getMessage()."Date: ". date("l jS \of F, Y, h:i:s A").". File: ". $_SERVER['REQUEST_URI'],1,"example@aol.com","From: example@yahoo.com");
         file_put_contents('last_email_sent.txt', time());
    }
    Code (markup):
     
    Masterful, Oct 29, 2014 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    Biggest problem I see is that this is 2014, not 2006, why are you still using the deprecated and soon to not even be supported in PHP mysql_ functions? You know, the reason for the GIANT RED WARNING BOXES in the manual? We've been told for EIGHT YEARS (pretty much since PHP 5 was introduced) to stop using them, the warning boxes were added two and a half years ago, WHY do people continue to use those inefficient and "insecure by design" functions?

    Of course, if you were using PDO around two-thirds your code would throw exceptions ALL ON THEIR OWN without resorting to the "or handle_error" nonsense.
     
    deathshadow, Nov 5, 2014 IP