开发者

Php, $result = mysql_query($query) or die ... how can i quickly email a fault?

I would like to add a simple, email me if theres a fault in the query feature.

开发者_如何学Python

After

$result = mysql_query($query) or die

What would I add ?


If there is a faulty SQL on frequent access page
or in the event of mysql server down,
is going to flood your inbox.

How about using error_log to log the faulty sql into local disk?
log rotate should be cater too (like log file can be Ymdh.log

Not really ASAP notification

  1. setup a cronjob run every minute
  2. do a line-count on log file
  3. email new lines since last minute
  4. if there is no new line, don't email
  5. store the line-count into a separate file for next execution

So, you will receive an email to indicate whether any new faulty SQL happen last minute, and no alert if nothing went wrong.

Since not able to convince you...

Then you can compare the faulty sql with the error log
If the faulty sql not found in the error log, append into error log, email the faulty sql

You would need to seek for a fashionable way to do string comparison fast in the error log (if the error log is growing too fast)

log rotate can be applied too


you can use a function say

$result = mysql_query($query) or reportErrorData()

function reportErrorData()
{
  $fp=popen("/usr/sbin/sendmail -t -oi","w");
  fwrite($fp,"To: DEV_EMAIL\n");
  fwrite($fp,"From: support@support.com\n");
  fwrite($fp,"Subject: System Error\n\n");

  fwrite($fp, "Message: error message\n\n")

  pclose($fp);
}

you can also pass in parameters to the reportErrorData function such as the sql, _FILE_, _FUNCTION_ etc.. also it will be handy to email any $_SESSION, $_GET or $_POST data, to see who the logged in user was if thats the case and to see where the error occoured i.e which file, function line etc.

hope this helps.


if(!$result = mysql_query($query){
    mail(...);
    die;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜