开发者

How to bind in PDO a string with %

开发者 https://www.devze.com 2022-12-13 02:12 出处:网络
I have the following query, (which don\'t work). How do I bid strings inside an existing str开发者_如何学运维ing with % (I believe the % is not the problem, but really not sure).

I have the following query, (which don't work).

How do I bid strings inside an existing str开发者_如何学运维ing with % (I believe the % is not the problem, but really not sure).

$sql="SELECT * FROM T WHERE f LIKE '%:bindParamString%'";


You can include % symbols into your value:

 $param = '%'.$param.'%';
 $query = "SELECT * FROM T WHERE f LIKE ?";

Or use SQL to concatenate string in database:

 ## if you have mysql
 $query = "SELECT * FROM T WHERE f LIKE CONCAT('%', ?, '%')";

It also a good idea to use LIKE instead of = then you're searching by patterns.


Try something like this:

$db = new PDO(...);

$sql = "SELECT * FROM T WHERE f=?";
$stmt = $db->prepare($sql);

$val = "%{$val}%";
$stmt->bindParam(1, $val, PDO::PARAM_STR);

For more info, I suggest to read the related doc page!

0

精彩评论

暂无评论...
验证码 换一张
取 消