Is it possible to bindParam WHERE name like %:name%

試著忘記壹切 提交于 2019-12-19 04:05:41

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!