我将一个类别数组传递给函数,我想进行此查询:
$sql = "SELECT *
FROM trans
WHERE id_user = $1 AND id_cat IN ($2)";
$value = array($id_user, implode(",", $categories));
$resource = pg_prepare($db, "get_trans", $sql);
$resource = pg_execute($db, "get_trans", $value);
但是pg_execute给了我一个错误
Warning: pg_execute(): Query failed: ERRORE: sintassi di input non valida per il tipo integer: "1,3,5,2,4,6,7" in /var/www/html/progetto-bd/application/function.php on line 370
答案 0 :(得分:3)
IN
构造需要行或集,而不是数组。
如果传递数组,请使用ANY
构造。
SELECT *
FROM trans
WHERE id_user = $1 AND id_cat = ANY ($2);
此外,Postgres数组文字的格式为'{elem1,elem2}'
。 (注意花括号。)
你需要传递两个参数,比如已经提到的@ toto21。
答案 1 :(得分:0)
您有两个值区别:$id_user
(字符串)和以逗号分隔的列表类别(内爆后的字符串)您的数组$value
应该只包含两个值。
试试这个:
$str_categories = implode(",", $categories);
$arr_value = array($id_user, $str_categories);
$resource = pg_prepare($db, "get_trans", $sql);
$resource = pg_execute($db, "get_trans", $arr_value);