0

I'm trying to get a PDO query running, so I'm doing:

$src = $this->conn->prepare("SELECT name, model, software FROM product WHERE 
                             model LIKE '%:search_string%' OR 
                             name LIKE '%:search_string%' OR 
                             software LIKE '%:search_string%'");
$src->bindParam(':search_string', $search_string);
$src->execute();
return $src->fetchAll();

But when I var_dump this, I always get an empty array ( [] ). However, if I change it to just "SELECT name, model, software FROM product", I get all of the products, just as expected, so how am I using the LIKE clause wrong? Or am I doing this completely wrong?

Toza
  • 1,348
  • 2
  • 14
  • 35

1 Answers1

3

Bound parameters cannot be used in this way. You have to input it as LIKE :search_string in the prepared query, then add the percent signs in the bound value (i.e. $src->bindParam(':search_string', '%' . $search_string . '%');).

See also this comment on PDOStatement::bindParam.

Another Code
  • 3,083
  • 21
  • 23
  • Yep, I did it when you linked me that documentation! Likes to you sir, as soon as the timer is off (6 minutes), I'll accept the answer. – Toza Sep 30 '15 at 08:17