myladeybugg
myladeybugg

Reputation: 329

MYSQL LIKE clause returning nothing?

I have a database with a column called item_name. I am trying to pull data out of my database using keywords in my item_name column using the LIKE clause but am getting nothing returned. Is there something wrong with my code below? I have tried commas and AND instead of the OR but still no results.

$sql = mysql_query("SELECT category, item_name, link, price, pic, retailer FROM products WHERE item_name LIKE ('%gtx%') OR ('%video%') OR ('%sound%') ORDER BY id DESC");

$query = ($sql) or die (mysql_error());

while ($result = mysql_fetch_array($query)) { 

Upvotes: 2

Views: 467

Answers (3)

Fernando Martinez
Fernando Martinez

Reputation: 197

Try using:
WHERE(item_name '%gtx%' OR item_name LIKE '%video%' OR item_name LIKE '%sound%') . I think this would run. Did you test your query directly in mysql interface?

Upvotes: 0

Skatox
Skatox

Reputation: 4284

You need to add LIKE after each OR, so the SQL setence will be like this:

SELECT category, item_name, link, price, pic, retailer FROM products 
WHERE item_name LIKE ('%gtx%') 
      OR item_name LIKE ('%video%') 
      OR item_name LIKE ('%sound%') 
ORDER BY id DESC

Because in the old sentence, without LIKE the database doesn't understand the ('%video%') term like a logic value.

Upvotes: 0

Taryn
Taryn

Reputation: 247690

you need to specify the LIKE after each OR

SELECT category, item_name, link, price, pic, retailer 
FROM products 
WHERE item_name LIKE ('%gtx%') 
    OR item_name LIKE ('%video%') 
    OR item_name LIKE ('%sound%') 
ORDER BY id DESC

Upvotes: 2

Related Questions