jeff5656 Posted December 8, 2010 Share Posted December 8, 2010 I tried to search for the answer on google, but it's hard to seach for "and" and "or". Anyway I want this query: $query = "select * from table where (term1= '$term1' or term2 like '$term2%') and user_id ='$id'"; such that ALWAYS limit the search to the user_id = $id, but search for either term1 or term2. Is the above query correct? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/221057-using-and-and-or-in-a-query/ Share on other sites More sharing options...
Maq Posted December 8, 2010 Share Posted December 8, 2010 Looks good, does it not work or something?. I find it weird you're using equals for one comparison, and LIKE for the other term. Quote Link to comment https://forums.phpfreaks.com/topic/221057-using-and-and-or-in-a-query/#findComment-1144607 Share on other sites More sharing options...
chrisshennan Posted December 8, 2010 Share Posted December 8, 2010 Hi The query looks fundamentally right and should always limit the search to the user_id but I agree with Maq that the part regarding term1 & term2 looks a bit weird in that you are using equals for one comparison and LIKE for another. Also, do you mean the LIKE clause to start with $term2 or be found anywhere in it? Maybe the following would work better (depending on what exactly you are using it for:- $query = "select * from table where (term1 LIKE '%$term1%' or term2 like '%$term2%') and user_id ='$id'"; Quote Link to comment https://forums.phpfreaks.com/topic/221057-using-and-and-or-in-a-query/#findComment-1144626 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.