graham23s Posted June 18, 2010 Share Posted June 18, 2010 Hi Guys, With this query, i only want to bring back results where the "in_stock" enum == 'Y' but the way it is now it brings back the product whether it's in stock or not: Query: $qS2 = "SELECT * FROM `fcpv3_products` WHERE `product_name` LIKE '%$searchString%' OR `product_description` LIKE '%$searchString%' AND `in_stock`='Y' ORDER BY `id`"; Is the query wrong? it look right lol chweers for any help guys Graham Quote Link to comment https://forums.phpfreaks.com/topic/205207-query-help/ Share on other sites More sharing options...
Psycho Posted June 18, 2010 Share Posted June 18, 2010 The problem is because you have three different criteria using an AND and an OR. The order of precedence on how those are processed is causing the issue. AND's are processed before OR's. http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html As the query is interpreted now it is pulling records where: `product_name` LIKE '%$searchString%' OR `product_description` LIKE '%$searchString%' AND `in_stock`='Y' You obviously want records where one of the first two conditions is true AND the last condition is true. So, you should use parens to force how the line is interpreted. SELECT * FROM `fcpv3_products` WHERE (`product_name` LIKE '%$searchString%' OR `product_description` LIKE '%$searchString%') AND `in_stock`='Y' ORDER BY `id` Quote Link to comment https://forums.phpfreaks.com/topic/205207-query-help/#findComment-1074147 Share on other sites More sharing options...
graham23s Posted June 18, 2010 Author Share Posted June 18, 2010 Thanks mj, that was the problem thanks again mate Graham Quote Link to comment https://forums.phpfreaks.com/topic/205207-query-help/#findComment-1074176 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.