我向函数传递一个类别数组,我想进行以下查询:
$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
IN
构造需要行或集合,而不是数组。
如果传递数组,请使用ANY
构造。
SELECT *
FROM trans
WHERE id_user = $1 AND id_cat = ANY ($2);
此外,Postgres 数组文字的形式为 '{elem1,elem2}'
。(请注意大括号。
你需要传递这两个参数,就像@toto21已经提到的。
您有两个不同的值:$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);