本文介绍了pdo准备带通配符的语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要执行以下mysql查询:
I want to execute the following mysql query:
SELECT * FROM `gc_users` WHERE `name` LIKE '%anyname%'
我尝试了这个但没有成功:
I tried this without success:
$stmt = $dbh->prepare("SELECT * FROM `gc_users` WHERE `name` LIKE :name");
$stmt->bindParam(':name', "%" . $name . "%");
$stmt->execute();
$stmt = $dbh->prepare("SELECT * FROM `gc_users` WHERE `name` LIKE '%:name%'");
$stmt->bindParam(':name', $name);
$stmt->execute();
所以我问你是否可以在准备好的语句中使用%通配符.
So I ask you if it is possible to use the % wildcard with prepared statements.
/编辑
谢谢.它与bindValue
一起使用:
$stmt = $dbh->prepare("SELECT * FROM `gc_users` WHERE `name` LIKE :name");
$stmt->bindValue(':name', '%' . $name . '%');
$stmt->execute();
推荐答案
它也可以通过以下方式与绑定参数一起使用:
It can work with bind param too in following way:
$name = "%$name%";
$query = $dbh->prepare("SELECT * FROM `gc_users` WHERE `name` like :name");
$query->bindParam(':name', $name);
$query->execute();
这篇关于pdo准备带通配符的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!