DBAL - symfony2为LIKE运算符绑定一个值

时间:2014-03-10 11:53:29

标签: symfony dbal

我正在尝试执行一个涉及带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

做错了

1 个答案:

答案 0 :(得分:3)

答案比我想象的要容易,就像亚当提出的那样

$stmt->bindValue("host", '%'.$host.'%');