-->

DBAL - Symfony2的结合对于像运营商的值(DBAL - symfony2 bind

2019-09-27 14:13发布

我试图执行一个SQL查询,涉及与DBAL LIKE运算符

基本上我的查询如下:

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我做得不对的bindValue

Answer 1:

答案是很容易,我想,像亚当建议

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


文章来源: DBAL - symfony2 bind a value for LIKE operator
标签: symfony dbal