Jump to content

Query help


graham23s

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/205207-query-help/
Share on other sites

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`

 

Link to comment
https://forums.phpfreaks.com/topic/205207-query-help/#findComment-1074147
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.