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

有帮助吗?

解决方案

The answer is easier than I thought, like Adam suggested

$stmt->bindValue("host", '%'.$host.'%');
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top