Search code examples
phpmysqlmysql-error-1064

displaying errors if mysql_query not successful


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


Solution

  • 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.