0

I have the following code that is not returning any results. When I change :search_term to just $search_term it will return one result as expected but when I use a wildcard it just returns 0.

$query = $db->getConnection()->prepare("SELECT * FROM `coils` WHERE coil_name IS LIKE '%:search_term%' ORDER BY id DESC LIMIT {$start}, {$perPage}");
$query->execute(array(
    ':search_term' => $search_term
));

echo $query->rowCount()

I have a feeling that I'm just not using it correctly. Any help would be great. Thanks!

Coilerz
  • 61
  • 1
  • 6

1 Answers1

0

You should not use quotes and build a string in the query, instead, you need to do that when you bind the value (without any quotes...):

SELECT * FROM `coils` WHERE coil_name LIKE :search_term ORDER BY ...

And then later on:

$query->execute(array(
    ':search_term' => '%' . $search_term . '%'
));

Also note that you should probably use LIKE and not IS LIKE.

jeroen
  • 91,079
  • 21
  • 114
  • 132