Jump to content

Operators not working correctly...


PHPNewbie55

Recommended Posts

I am having a problem getting a small snippet of code to work correctly....
Here is my code:::

$pol = mysql_query("select * from lma_links where (
	ON_SALE = 'Y' and
	SUB = '159' or SUB = '111' or SUB = '71'
	) order by rand() desc limit $addrandlistcount ");

It works... but the results sometimes contain items where the ON_SALE does not equal 'Y'

Any help figuring this out would be greatly appreciated....
::)

Link to comment
https://forums.phpfreaks.com/topic/282171-operators-not-working-correctly/
Share on other sites

You need to use parenthesis to force the order of your conditions. Right now, that statement will be evaluated like this order:

(ON_SALE = 'Y' and SUB = '159') or SUB = '111' or SUB = '71'
So as long as either sub=111 or sub=71 is true, the entire condition is true. The ON_SALE=Y condition is attached only to the SUB=159 condition. I'm assuming you want that ON_SALE=Y to be required regardless, but sub can be any of the values. You need to use parenthesis around the OR conditions to ensure they are evaluated first, then only if they are true, the on_sale=y is evaluated.

 

ON_SALE = 'Y' and (SUB = '159' or SUB = '111' or SUB = '71')

Archived

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

×
×
  • Create New...

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.