McMaster Posted July 3, 2010 Share Posted July 3, 2010 Hey, I am trying to create a search query in MySQL where it will search the description and title from the database products. Now the only problem is that it doesn't actually search within that category I choose from the drop down but instead it searches all categories. Anyone know what I am doing wrong? This is my code: $category = $_POST['category']; $sql = "SELECT * FROM products WHERE category = '$category' AND description LIKE '%$cat_new%' OR title LIKE '%$cat_new%' OR keyword1 LIKE '%$cat_new%' OR keyword2 LIKE '%$cat_new%' OR keyword3 LIKE '%$cat_new%' OR keyword4 LIKE '%$cat_new%'"; [code=php:0] I tried echoing the $category variable and the category I choose from the drop down is showing. Any help appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/206642-small-problem-with-a-query/ Share on other sites More sharing options...
Pikachu2000 Posted July 3, 2010 Share Posted July 3, 2010 I'm not a search expert, but I think this might be a precedence issue in the query string. Try forcing the precedence with parentheses. $sql = "SELECT * FROM products WHERE category = '$category' AND (description LIKE '%$cat_new%' OR title LIKE '%$cat_new%' OR keyword1 LIKE '%$cat_new%' OR keyword2 LIKE '%$cat_new%' OR keyword3 LIKE '%$cat_new%' OR keyword4 LIKE '%$cat_new%')"; Quote Link to comment https://forums.phpfreaks.com/topic/206642-small-problem-with-a-query/#findComment-1080752 Share on other sites More sharing options...
McMaster Posted July 3, 2010 Author Share Posted July 3, 2010 Yeah thanks but that didn't work any more help? Quote Link to comment https://forums.phpfreaks.com/topic/206642-small-problem-with-a-query/#findComment-1080755 Share on other sites More sharing options...
PFMaBiSmAd Posted July 3, 2010 Share Posted July 3, 2010 Define: that didn't work? What did it do, because that is exactly how you would make the query so that the specific category would match AND any one of the other terms would match. Are you sure you made the code change in the actual file you are using? Quote Link to comment https://forums.phpfreaks.com/topic/206642-small-problem-with-a-query/#findComment-1080756 Share on other sites More sharing options...
McMaster Posted July 3, 2010 Author Share Posted July 3, 2010 Wait it did work thanks a lot for that. Quote Link to comment https://forums.phpfreaks.com/topic/206642-small-problem-with-a-query/#findComment-1080758 Share on other sites More sharing options...
Pikachu2000 Posted July 3, 2010 Share Posted July 3, 2010 Certainly. Glad I could help! Quote Link to comment https://forums.phpfreaks.com/topic/206642-small-problem-with-a-query/#findComment-1080759 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.