3

如果我有一个正常工作的 postgres 函数(类似于下面);

package."func_SetValues"(id integer, values integer[])
    DECLARE
        i integer;
    BEGIN
    FOR i IN 1..array_upper(values, 1) 
      LOOP
       EXECUTE package."func_DoStuff"(id, values[i]);
      END LOOP;
    END
    LANGUAGE 'plpgsql' VOLATILE

并且,一个 PHP 脚本 doStuff.php 调用此函数,该函数接受并使用这些值作为 url 编码参数。

$dbconn = pg_connect("host=192.168.1.222 port=5432 dbname=betya user=betya_user password=bettyboo")
    or die('Could not connect: ' . pg_last_error());
$query = 'SELECT * FROM package."func_SetValues"($1, $2)';
$result=pg_prepare($dbconn, "qy", $query);
$paramater1=$_REQUEST['id'];    
$paramater2=$_REQUEST['arr']; 
$result=pg_execute($dbconn, "qy", array($paramater1, $paramater2));

while($e=pg_fetch_assoc($result))
$output[]=$e;

print(json_encode($output));
pg_free_result($result);
pg_close($dbconn);

为了测试 PHP 脚本,以下 URL 是否以 PHP 可以“请求”值并可以传递给 postgres 的方式正确编码数组 'arr'?或者脚本中是否需要进一步的功能才能正确传递给 pg_execute 命令?

http://192.168.1.50/server/doStuff.php?id=1790&arr[]=1788&arr[]=1790&arr[]=1805
4

1 回答 1

1

是的,需要更多代码。该数组必须经过非平凡的转换才能作为参数传递,因为 PG 期望它以文档中所述的文本形式出现: http ://www.postgresql.org/docs/current/static/arrays.html#ARRAYS -IO

In another question: PHP array to postgres array , a PHP function to that effect has been already submitted to S.O., it might just be what you need (and ever better since it supports recursive arrays).

于 2012-04-04T14:53:52.100 回答