DBAL - symfony2 bind a value for LIKE operator

青春壹個敷衍的年華 提交于 2019-12-11 05:31:56

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!