简体   繁体   English

如何将数组编码为 URL 参数,在 PHP 中提取并作为 function 参数传递给 Postgres

[英]How to encode an array as URL parameter, extract in in PHP and pass to Postgres as function parameter

If I have a correctly working postgres function (similar to below);如果我有一个正常工作的 postgres function(类似于下面);

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

And, a PHP script, doStuff.php, calling this function which accepts and uses the values as url encoded parameters.并且,一个 PHP 脚本,doStuff.php,调用这个 function,它接受并使用值作为 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);

To test the PHP script, does the following URL correctly encode the array 'arr' in a way that PHP can "Request" the values and can pass to postgres in away that it understands?要测试 PHP 脚本,以下 URL 是否以 PHP 可以“请求”值并以它理解的方式传递给 postgres 的方式正确编码数组“arr”? Or is further functionality required in the script to correctly pass to the pg_execute command?还是脚本中需要更多功能才能正确传递给 pg_execute 命令?

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

Yes, further code is required.是的,需要进一步的代码。 The array has to be non-trivially converted to be passed as a parameter, because PG expects it in textual form as described in the doc: http://www.postgresql.org/docs/current/static/arrays.html#ARRAYS-IO该数组必须经过非平凡的转换才能作为参数传递,因为 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 SO, it might just be what you need (and ever better since it supports recursive arrays).在另一个问题中: PHP array to postgres array ,一个 PHP function 已经提交给 SO,它可能正是你所需要的(而且更好,因为它支持递归数组)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM