jribeiro
jribeiro

Reputation: 3463

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

Upvotes: 0

Views: 3217

Answers (2)

Liam Allan
Liam Allan

Reputation: 1115

or something simple like:

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

Upvotes: 0

Mike Purcell
Mike Purcell

Reputation: 19979

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.

Upvotes: 1

Related Questions