问题
I'm testing a small search feature:
But I've come across an error that I cannot seem to solve. You can see the PDO query 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 the parameter %:name%
from my query, but I don't know if that's actually possible?
I receive the error:
Uncaught exception 'PDOException' with message 'SQLSTATE[42000]:.....
And I can see in the error that '' has been put around test1 %'test1'%
Is what I'm trying 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 . "%"));
回答1:
Use
LIKE CONCAT('%', :name, '%')
来源:https://stackoverflow.com/questions/27051295/is-it-possible-to-bindparam-where-name-like-name