Is it possible to bind a Param WHERE name as%: name%

I am testing a small search function:

But I ran into an error that I cannot solve. You can see the PDO request here:

$search = "test1"; //later to be changes to $_POST ['search'];

$sql = "SELECT id, name FROM clients WHEE name like %:name% order by id LIMIT 5";
$stm = $db->prepare ( $sql );
$stm->bindParam ( ":name" , $search);
$result = $stm->execute ();

      

As you can see, I'm trying to bind a parameter %:name%

from my request, but I don't know if this is actually possible?

I am getting the error:

Undefined exception "PDOException" with message "SQLSTATE [42000]: .....

And I see in error that '' is placed around test1% 'test1'%

Is what I am trying to do possible, or do I need to do something like this?

$query = "SELECT id, name FROM clients WHEE name like :name order by id LIMIT 5";

$sql->execute(array(":name" => "%" .$search . "%"));

      

+3


source to share


1 answer


Use



LIKE CONCAT('%', :name, '%')

      

+6


source







All Articles