PDO使用通配符预处理语句(pdo prepared statements with wildcar

2019-09-01 06:49发布

我想执行下列MySQL查询:

SELECT * FROM `gc_users` WHERE `name` LIKE '%anyname%'

我想这没有成功:

$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();

所以我问你,如果它是可以使用通配符%,与准备好的语句。

/编辑

谢谢。 它与工作bindValue

$stmt = $dbh->prepare("SELECT * FROM `gc_users` WHERE `name` LIKE :name");
$stmt->bindValue(':name', '%' . $name . '%');
$stmt->execute();

Answer 1:

它可与绑定PARAM太以下方式工作:

$name = "%$name%";
$query = $dbh->prepare("SELECT * FROM `gc_users` WHERE `name` like :name");
$query->bindParam(':name', $name);
$query->execute();


Answer 2:

这可能是一种替代方案:

$className = '%' . $this->className . '%';
$query->bind_param('s', $className);


文章来源: pdo prepared statements with wildcards