Jump to content

Archived

This topic is now archived and is closed to further replies.

JustinK101

Whats the deal with mysql_query() || or OR

Recommended Posts

Hello, I know this makes me seem stupid, but I have been coding in PHP for about two years, and always had this question.

For the longest time when doing the mysql_query() call I would never use the die condition to check for failure because I thought it didnt work when returning a result set.

I.E.  $result = mysql_query($sql) || die();

Then one day I tried replaced the || with the words OR and it works. It seems the || doesnt allow the result variable to get filled but the OR allows it. Is it me, or does this baffle anybody else?

Share this post


Link to post
Share on other sites
Well yeah I know its shorthand, but there is a difference || caused the $result result variable to not be stored when OR allows the result variable to be stored.

Share this post


Link to post
Share on other sites
I honestly haven't found any specific reason [i]why[/i] it won't let you, other than it wasn't programmed that way.  My best guess is that it simply wasn't programmed to be parsed the same, in that circumstance.

Share this post


Link to post
Share on other sites
Seems the cause is related to operator presedence.

Here try doing a query like:

$sql = "SELECT first_name FROM test";
$result = mysql_query($sql) || die("Failure!");
//$result is not defined here, even though the query is valid.

$sql = "SELECT first_name FROM test";
$result = mysql_query($sql) OR die("Failure!");
//$result IS defined here

Share this post


Link to post
Share on other sites
from a comment in the operators precedence section of [url=http://us2.php.net/manual/en/language.operators.php#language.operators.precedence]the manual[/url]:

[quote]
<?php $a = $b && $c; ?>
<?php $a = $b AND $c; ?>

the first code will set $a to the result of the comparison $b with $c, both have to be true, while the second code line will set $a like $b and THAN - after that - compare the success of this with the value of $c
[/quote]

Share this post


Link to post
Share on other sites
'||' is a boolean operator, and it always results in either true or false. So in your example above, $result would become TRUE instead of being a query result (as long as the query was valid, in which case die() would be called). The 'OR' operator always returns the value of the first operand, and it will evaluate operands until it either finds one that can resolve to TRUE, or it runs out of operands. Sound confusing? It is. Maybe this example will help:

$result = "0" || 0 || false || "hello"; // $result becomes TRUE
$result = "0" OR 0 OR false OR "hello" ; $result becomes "0"

Just don't use '||' to do what you're trying to do.

Share this post


Link to post
Share on other sites
Yeah thanks guys, I think this is a very common problem for starting PHP and MySQL users, so hopefully cleared up some confusion. ALWAYS use OR with the mysql_query() function call.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.