ryanfilard Posted September 13, 2011 Share Posted September 13, 2011 I am trying to make my database search only search where private = '$priiv'(Usually 0,1, or 2) but it does not work $query_search = "SELECT * FROM users WHERE username LIKE '%$idea%' OR fname LIKE '%$idea%' OR lname LIKE '%$idea%' OR tags LIKE '%$idea%' AND private = '$priiv'"; What am i doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/247085-php-sql-question/ Share on other sites More sharing options...
Pikachu2000 Posted September 13, 2011 Share Posted September 13, 2011 What about it "does not work"? Quote Link to comment https://forums.phpfreaks.com/topic/247085-php-sql-question/#findComment-1268941 Share on other sites More sharing options...
ryanfilard Posted September 13, 2011 Author Share Posted September 13, 2011 AND private = '$priiv' Quote Link to comment https://forums.phpfreaks.com/topic/247085-php-sql-question/#findComment-1268942 Share on other sites More sharing options...
DavidAM Posted September 13, 2011 Share Posted September 13, 2011 AND takes precedence over OR so basically, that statement says: SELECT * FROM users WHERE username LIKE '%$idea%' OR fname LIKE '%$idea%' OR lname LIKE '%$idea%' (OR tags LIKE '%$idea%' AND private = '$priiv') So it only checks the private column when checking the tags column. You need to use parenthesis to group the OR's all together: SELECT * FROM users WHERE (username LIKE '%$idea%' OR fname LIKE '%$idea%' OR lname LIKE '%$idea%' OR tags LIKE '%$idea%') AND private = '$priiv' or, rearrange it to something easier to read (IMO) SELECT * FROM users WHERE private = '$priiv' AND (username LIKE '%$idea%' OR fname LIKE '%$idea%' OR lname LIKE '%$idea%' OR tags LIKE '%$idea%') Quote Link to comment https://forums.phpfreaks.com/topic/247085-php-sql-question/#findComment-1268968 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.