Pregunta

        $in = implode(',', array_fill(0, count($folioIds), '?'));

        $statement = $this->connection->prepare("SELECT * FROM page WHERE folio_id IN(". $in .") AND content LIKE ?");

        foreach ($folioIds as $k => $id) {
            $statement->bindValue($k+1, $id);
        }
        $statement->bindValue($k+1, "%" . $q . "%");

        if($statement->execute()) {
            print_r($statement->fetchAll());
            $statement->debugDumpParams();
        }

I'm trying to use both In and Like operator in a query, and it's throwing invalid number of tokens error.

Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' 

Also could we use both named and ? parameters, my attempt using that failed as well.

¿Fue útil?

Solución

I think $k is undefined outside the foreach loop

It's probably easier like this:

 $statement = $this->connection->prepare(
        "SELECT * FROM page WHERE folio_id IN(". $in .") AND content LIKE ?");
 $values=$folioIds;
 $values[]="%" . $q . "%";
 if ($statement->execute($values)){
    $rows=$statement->fetchAll();
 }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top