我正在尝试执行一个涉及带DBAL的LIKE运算符的SQL查询
基本上我的查询如下:
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
做错了答案 0 :(得分:3)
答案比我想象的要容易,就像亚当提出的那样
$stmt->bindValue("host", '%'.$host.'%');