displaying errors if mysql_query not successful

瘦欲@ 提交于 2019-12-24 23:23:30

问题


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

I call it like this:

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

And the function is:

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)

What i doing anything wrong?

Thanks


回答1:


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

$result = mysql_query($sql);

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

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.




回答2:


or something simple like:

mysql_query($sql) or die(dbErrors($sql));


来源:https://stackoverflow.com/questions/8142087/displaying-errors-if-mysql-query-not-successful

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!