JustinK101 Posted October 3, 2006 Share Posted October 3, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/22827-whats-the-deal-with-mysql_query-or-or/ Share on other sites More sharing options...
.josh Posted October 3, 2006 Share Posted October 3, 2006 it's a shorthand method of coding a condition. kind of like the ternary operator. Quote Link to comment https://forums.phpfreaks.com/topic/22827-whats-the-deal-with-mysql_query-or-or/#findComment-102846 Share on other sites More sharing options...
JustinK101 Posted October 3, 2006 Author Share Posted October 3, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22827-whats-the-deal-with-mysql_query-or-or/#findComment-102847 Share on other sites More sharing options...
.josh Posted October 3, 2006 Share Posted October 3, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22827-whats-the-deal-with-mysql_query-or-or/#findComment-102851 Share on other sites More sharing options...
JustinK101 Posted October 3, 2006 Author Share Posted October 3, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/22827-whats-the-deal-with-mysql_query-or-or/#findComment-102852 Share on other sites More sharing options...
.josh Posted October 3, 2006 Share Posted October 3, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/22827-whats-the-deal-with-mysql_query-or-or/#findComment-102857 Share on other sites More sharing options...
moxicon Posted October 3, 2006 Share Posted October 3, 2006 '||' 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. Quote Link to comment https://forums.phpfreaks.com/topic/22827-whats-the-deal-with-mysql_query-or-or/#findComment-102861 Share on other sites More sharing options...
JustinK101 Posted October 3, 2006 Author Share Posted October 3, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22827-whats-the-deal-with-mysql_query-or-or/#findComment-102866 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.