如果mysql_query不成功则显示错误 [英] displaying errors if mysql_query not successful

查看:95
本文介绍了如果mysql_query不成功则显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个调试功能,通过电子邮件向我发送mysql错误并在查询不成功时执行查询.

I created a debug function to email me the mysql error and query executed if a query is not successful.

我这样称呼它:

mysql_query($sql) or $this->debug->dbErrors($sql);

函数是:

function dbErrors($sql = ''){
    if($this->doDebug)
        echo mysql_error()."<br/>".$sql;
    else
        @mail(hidden_email,$_SERVER['HTTP_HOST'].' Mysql Error','A error occured in '.$_SERVER['HTTP_HOST'].':<br/>'.mysql_error().'<br/>'.$sql);
}

问题是,即使查询执行得很好(至少插入了数据并且一切正常,也可以)我仍收到电子邮件

The problem is that i'm receiving emails even when the query executes fine (at least the data is inserted and everything works out ok)

我做错了什么?

谢谢

推荐答案

该或"结构可能会引起问题,我将执行以下操作:

That 'or' construct may be causing issue, I would do something like:

$result = mysql_query($sql);

if (!$result) {
     $this->debug->dbErrors($sql);
}

这样,您将进行显式检查,以查看$ result是布尔假(查询无效)还是资源(查询有效).关键是仅在确实存在问题时调用$this->debug->dbErrors(),否则,将以电子邮件方式发送每个查询的代码编写方式.

This way you are doing an explicit check to see if $result is a boolean false (query is invalid), or a resource (query is valid). The point is to only call on $this->debug->dbErrors() if there indeed is an issue, otherwise the way your code is written, every query will be emailed.

这篇关于如果mysql_query不成功则显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆