PHP - Wrong SELECT - Query

I have this code:

$r=mysql_query("SELECT * FROM advertisements WHERE exposure!='0' AND `status`='2' AND clicks_left_micro>0 OR clicks_left_mini>0 OR clicks_left_standard>0 OR clicks_left_extended>0");

The above code, should ONLY get the 2 first rows, in my database. (Please check the pic below): http://i51.tinypic.com/dejiw.png

Why does it select all 3 rows, when I specifically say status="2"?

How can I fix this?

Thanks in advance.

Upvotes: 0

Views: 56

Answers (3)

johangu
johangu

Reputation: 824

Try encapsling the OR part in parentheses

AND clicks_left_micro>0 OR clicks_left_mini>0 OR 
clicks_left_standard>0 OR clicks_left_extended>0

changed to

AND (clicks_left_micro>0 OR clicks_left_mini>0 OR 
clicks_left_standard>0 OR clicks_left_extended>0)

should solve it.

Upvotes: 0

Eric Petroelje
Eric Petroelje

Reputation: 60498

Here is what you have:

SELECT * FROM advertisements 
WHERE exposure!='0' 
AND `status`='2' 
AND clicks_left_micro>0 
OR clicks_left_mini>0 
OR clicks_left_standard>0 
OR clicks_left_extended>0

But I think that this is what you actually wanted:

SELECT * FROM advertisements 
WHERE exposure!='0' 
AND `status`='2' 
AND (clicks_left_micro>0 
OR clicks_left_mini>0 
OR clicks_left_standard>0 
OR clicks_left_extended>0)

Upvotes: 4

Nicola Cossu
Nicola Cossu

Reputation: 56357

You have to use parenthesis to respect boolean operators priority.

Upvotes: 0

Related Questions