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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.