问题
I am trying to execute a sql query that involves a LIKE operator with DBAL
Basically my query is the following:
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'%
Obiviously I'm doing something wrong with the bindValue
回答1:
The answer is easier than I thought, like Adam suggested
$stmt->bindValue("host", '%'.$host.'%');
来源:https://stackoverflow.com/questions/22299364/dbal-symfony2-bind-a-value-for-like-operator