Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use bindValue with LIKE operator in SQL query? [duplicate]

Tags:

sql

php

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.

like image 860
Diar Avatar asked Sep 10 '25 21:09

Diar


1 Answers

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.

like image 64
GMB Avatar answered Sep 13 '25 11:09

GMB