1
$query = sprintf("select * from sometable;");
$result = mysql_query($query) or die (mysql_error());

modified to:

$query = sprintf("select * from sometable;");
$result = mysql_query($query) or $DBError=true;

Now I want to execute 2 statements if the query fails, is this possible using the "short above"? e.g. something like this:

$query = sprintf("select * from sometable;");
$result = mysql_query($query) or {$DBError=true; $ErrorCode=0;}
2
  • Do you want to set result always as true or do you want to set the variables $DBError and $ErrorCode after the execution of mysql_query? Notice that documentation says that mysql_query should NOT end with ; (php.net/manual/en/function.mysql-query.php) Commented Jun 24, 2016 at 13:40
  • 1
    Just an offtopic tip: mysql library is deprecated and already removed in PHP 7. You should use the mysqli library (i for improved). Commented Jun 24, 2016 at 14:00

1 Answer 1

2

You can simply use conditional statement like:

$result = mysql_query($query);
if (!$result) {
 $DBError = true;
 $ErrorCode = 0;
}

By the way. mysql_ extension is deprecated. You should use PDO.

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

2 Comments

Yes, mysql is deprecated, but mysqli is not. I wouldn't recommend a complete architecture change, as it's unrelated to the OP's question.
I'm using version 5 but will look into it

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.