Jump to content

Operators not working correctly...


PHPNewbie55
Go to solution Solved by kicken,

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
Share on other sites

  • Solution

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')
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.