DBAL-symfony2为LIKE运算符绑定一个值 [英] DBAL - symfony2 bind a value for LIKE operator
本文介绍了DBAL-symfony2为LIKE运算符绑定一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用DBAL执行涉及LIKE运算符的SQL查询
I am trying to execute a sql query that involves a LIKE operator with DBAL
基本上我的查询如下:
public function getSubsiteByHostname($host){
$sql = "SELECT A.id, A.title, A.layout_id
FROM sites AS A
LEFT JOIN layouts B
ON A.layout_id = B.id
WHERE A.baseurl LIKE '%:host%'
";
$stmt = $this->db->prepare($sql);
$stmt->bindValue("host", $host);
$stmt->execute();
return $stmt->fetch();
}
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%'hostname.dev'%
显然我对bindValue做错了事
Obiviously I'm doing something wrong with the bindValue
推荐答案
答案比我想象的要容易,就像亚当建议的那样
The answer is easier than I thought, like Adam suggested
$stmt->bindValue("host", '%'.$host.'%');
这篇关于DBAL-symfony2为LIKE运算符绑定一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文