2

I've been trying to replace the value in '%:value%' when I use the LIKE operator in my query.

I have also tried using CONCAT() but that didnt work either.

    $query = "SELECT * 
              FROM books
              WHERE title LIKE '%:title%'";
    ...
    ...
    statement->bindValue(':title', $title, PDO::PARAM_STR);

:title should be replaced with the variable $title but it doesnt. The query is working fine but the :title just doesnt get replaced.

Diar
  • 123
  • 1
  • 8

2 Answers2

3

You probably want :

$query = "SELECT * 
          FROM books
          WHERE title LIKE CONCAT( '%', :title, '%')";
...
...
statement->bindValue(':title', $title, PDO::PARAM_STR);

The bind parameter should be used as a litteral string. CONCAT can be used to concatenate the parameter with percent signs on both ends.

GMB
  • 216,147
  • 25
  • 84
  • 135
0

Did you try using concat() like this?

SELECT * 
FROM books
WHERE title LIKE CONCAT('%', :title, '%')
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786