Take the 2-minute tour ×

I want to do something like "Where conditions = '$condition' OR conditions = '$condition2' OR conditions = '$condition3' but that returns all entries in the database versus the ones i am looking for.. what i can i do to make this work?

$sql = "SELECT STRAIGHT_JOIN DISTINCT table1.person_id, table1.full_name, 
table2.stuff_id, table2.stuff_name, table3.whatever_why, 
table3.whatever_comments, table3.whatever_test_id
from table1 
join table4 on table1.person_id = table4.person_id 
join table2 on table4.stuff_id  = table2.stuff_id
join table3 on table1.person_id = table3.person_id 
WHERE conditions = '$condition' and ( (
     table3.whatever_why like '%$q%'
  OR table3.whatever_comments like '%$q%'
  OR table3.whatever_name like '%$q%'
order by table3.id DESC limit $startrow, 10";

$result = mysql_query($sql);
share|improve this question
is conditions ='$condition' a necessary condition? or one of the conditionA OR conditionB ones? –  Jan S Sep 16 '11 at 13:00
BTW, you definitely want to sanitize $q and the other variables before you pass it into the query, to avoid Bobby Tables (SQL injection). –  Piskvor Sep 16 '11 at 13:00
@piskvor yeas i am sanitizing. this is just a snippet of the code. –  KPO Sep 16 '11 at 13:02
all 3 are necessary to sort through. –  KPO Sep 16 '11 at 13:03
add comment

2 Answers

up vote 3 down vote accepted

you may use parenthesis to group up your logic.

just continuing to list OR statements will match the rows if any of those are true.

share|improve this answer
add comment
Where conditions in ('$condition1', '$condition2', '$condition3')
share|improve this answer
add comment

Your Answer


