Search code examples
mysqlsql-like

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)) { 

Solution

  • 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