0
public function search($search)
{
    global $pdo;
    $query = $pdo->prepare('SELECT * FROM items WHERE item_name = ?');
    $query->bindValue(1, $search);
    $query->execute();
    return $query->fetchAll();
}

with that function if I called it, and say $search was cookie.

and inside my database I have

chocolate chip cookie
oatmeal cookie
cookie 

it would only return cookie So how could I fix my query so it returns all the item_names that are a partial match or contain cookie also? Thanks.

Zera42
  • 2,592
  • 1
  • 21
  • 33

2 Answers2

3

You would use like:

where item_name like '%cookie%'

If you want to look for a full word match:

where concat(' ', item_name, ' ') like '% cookie %'
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
3

For completeness, or as an alternative to LIKE, you could use LOCATE() or INSTR():

SELECT * FROM table WHERE LOCATE('cookie', item_name);

Note: If you're curious about performance, here's some old benchmarks.

Community
  • 1
  • 1
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174