如何将 IN 子句的一组值传递给 pg_execute() 数组

How to pass a set of values for IN clause to pg_execute() arrays

我向函数传递了一个类别数组,我想进行以下查询:

$sql = "SELECT * 
        FROM trans   
        WHERE id_user =  AND id_cat IN ()";

$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

您有两个不同的值:$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);

IN 构造需要 ,而不是数组。
如果传递数组,请使用 ANY 构造。

SELECT * 
FROM   trans   
WHERE  id_user =  AND id_cat <b>= ANY ()</b>;

此外,Postgres 数组文字的形式为 '{elem1,elem2}'。 (注意大括号。)

并且你需要传递两个参数,就像@toto21已经提到的那样。