Jump to content


Photo

Whats the deal with mysql_query() || or OR


  • Please log in to reply
7 replies to this topic

#1 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 03 October 2006 - 04:04 AM

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?

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 03 October 2006 - 04:28 AM

it's a shorthand method of coding a condition.  kind of like the ternary operator.


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 03 October 2006 - 04:31 AM

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.

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 03 October 2006 - 04:34 AM

I honestly haven't found any specific reason why 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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 03 October 2006 - 04:34 AM

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



#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 03 October 2006 - 04:47 AM

from a comment in the operators precedence section of the manual:

<?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


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 moxicon

moxicon
  • New Members
  • Pip
  • Newbie
  • 3 posts
  • LocationPortland, Oregon, USA

Posted 03 October 2006 - 04:58 AM

'||' 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.
Moxley
[br]www.moxleystratton.com

#8 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 03 October 2006 - 05:22 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users