4

I don't want to "die()" when it concerns only a small part of the script and I tried:

$result = mysql_query($sql) or echo("error with responses");

But it generated an error. How do I just give out an error message and continue to execute the rest of the script since even if it fails to connect, the rest should not be affected.

4
  • 4
    The question title nearly gave me an aneurysm. Commented May 10, 2011 at 20:39
  • See, now that's a title to get views/responses. Generally only a fatal error (often, syntax errors and the like) should kill a script. What was the specific error message you got? Commented May 10, 2011 at 20:41
  • actually, it's funny, I didn't pay attention to my title after I looked at the list of questions asked and then I asked. lol I didn't want to terminate the script just because of a mysql error. I wanted to keep executing the rest of the script even if it fails to get data from db. Commented May 10, 2011 at 20:47
  • I've updated the title, as the previous one seemed (at best) irrelevant. :-) Commented May 10, 2011 at 20:49

7 Answers 7

5
$result = mysql_query($sql);
if(mysql_error()) {
  echo "Error: " . mysql_error();
}
Sign up to request clarification or add additional context in comments.

2 Comments

@netrox So accept this answer, your accept ratio will be higher.
im waiting for 10 minutes to expire :/
5

In terms of your original question title - you can't. The purpose of die and exit are to terminate script processing. If you don't want to terminate script processing, don't call die or exit.

In terms of suppressing the output of errors, this is possible using the error control operator (@) but in-advisable. (You must check for error state yourself if you're suppressing errors in this manner.)

For example, you could use:

$result = @mysql_query($sql);
if(is_resource($result)) {
    // The query worked...

}
else {
   // Handle error state, perhaps using mysql_error
}

However, just to make this clear, NEVER casually add the error suppressor to a function call. You MUST ensure you're handling any potential errors correctly yourself.

Comments

2
$result = mysql_query($sql) or print("error with responses");

Echo cannot be used in logical comparisons such as what you're trying to do. You should use print instead, that won't cause PHP errors.

Comments

1

Check the return type of mysql_query

$result = mysql_query($sql);
if ($result===false) {

 echo 'The Query failed';
}

//> Continue

Comments

1

You don't have to use die as the second function call, it's just used as a convention in almost every mysql_query tutorial out there.

All we're doing is evaluating a boolean expression. $result = $a OR $b. If the value of $a is true, then we never execute $b because we don't need to. We've satisfied the logical OR with one side of the operator being true, which is all we need.

The die() function immediately exits the script with a message. You could replace it with your own function to print a pretty error message and continue through the script without exiting., e.g:

function my_error_function($status) {
    echo "ERROR: $status";
}

$result = mysql_query($sql) or my_error_function("Uhoh.  There was a problem executing $sql.");

Comments

0

You can turn off error reporting, which you want to do anyway in a production environment.

error_reporting(0);

It will still throw an error, but it will no longer be shown.

This is very unusual, though. I can't think of a reason why one would want to accept a mySQL error - if the problem is broken SQL, you would usually catch it beforehand.

4 Comments

MySQL errors can be good - if you get a duplicate key error, for example, you'd present the user with a message that their username/URL/whatever already exists and continue on.
@ceejayoz I would tend to want to catch that beforehand. I don't like showing raw SQL error messages to end users, not even if the server is unreachable.
You don't have to show the raw error message. Use mysql_errno() to figure out what the error was, then handle it.
@ceejayoz yeah, then we're on the same page. I thought you meant passing errors through directly to the user
0

You should be looking at try / catch not die() or exit

2 Comments

Alas, the decrepit mysql extension doesn't throw exceptions, so there's nothing to try/catch. This'd be correct if PDO was being used.
@charles: yet another reason to use a wrapper layer to make it throw exceptions, or use errorexceptions. So there are ways to make it work...

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.