Pregunta

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

¿Fue útil?

Solución

The answer is easier than I thought, like Adam suggested

$stmt->bindValue("host", '%'.$host.'%');
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top