1

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
  • 113
  • 8

2 Answers2

1

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
  • 195,563
  • 23
  • 62
  • 110
0

Did you try using concat() like this?

SELECT * 
FROM books
WHERE title LIKE CONCAT('%', :title, '%')
Gordon Linoff
  • 1,198,228
  • 53
  • 572
  • 709