How to use an array in query()

3 months ago

netteman
Member | 50
+
0
-

Hi,

I'd like to do this

$arr = array("x" => 1, "y" => 2);
$this->db->query("SELECT * FROM text WHERE id = ? OR id = ?", $arr);

but Nette says “There are more placeholders than passed parameters”.

Is it possible to use an array of values to fill the ? in the query?

Thanks :)

3 months ago

CZechBoY
Member | 2459
+
+1
-

Use method queryArgs, which uses 1 parameter as 1 item in array. Method query uses 1 function parameter as 1 query parameter – so you passed 1 array parameter instead of 2.

3 months ago

netteman
Member | 50
+
0
-

Thank you!

For others: this code works

$arr = array(1, 2);
$this->db->queryArgs("SELECT * FROM texty WHERE id_text = ? OR id_text = ?", $arr);