0

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

3
  • Do you get any error in the email sent to you ? Commented Nov 15, 2011 at 19:45
  • @AurelioDeRosa doDebug is just a private php var to set the mode how the errors are displayed depending on development or production state. It let's debugging print messages to screen or to email. Thanks Commented Nov 16, 2011 at 2:15
  • @Interstellar_Coder Hi I get errors for (almost) all (did't have time to check) queries. even the ones i'm sure are working and where the data is correctly inserted in db!! Commented Nov 16, 2011 at 2:17

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

hi!! Thanks for the answer. I don't know why the downvote. Anyway, I thought the or would only execute the second expression if the first where true. By logic an or always returns true if one of the conditions is true, right? If the first one is true what is the point in continuing with checks? But even if php indeed continues to evaluate the question you're not the developer ;) I'll check tomorrow! Thanks
0

or something simple like:

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

1 Comment

Thanks for the answer Liam but the point of the debug function (actually is a function in a class) is to not die unless it's critical. Anyways I do not see how that would be different from what I'm doing. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.