在PHP中处理Postgres输出函数参数
I have a Postgres database I wish to access. I need to call several functions that exist in the DB. I have achieved connection to the database and running queries. I have also achieved to call the functions I want. My problem is that when a Postgres function has more than one OUT parameters, instead of returning an array I can access either with the offset or with the row name, it instead returns a string with both the OUT parameters:
$query = "Select pg_function('" . Getenv("REMOTE_ADDR") . "')";
$result = pg_query($query);
$line = pg_fetch_array($result, NULL, PGSQL_ASSOC);
var_dump($line);
What var_dumb returns is this:
array
'ua_bl_session_request' => string '(c6787f5f2b2c885168162c8c8ffff220,8fe04393-c188-0e08-b710-a2ce89066768)' (length=71)
I can of course parse that string but is there a way to achieve what I want and I am missing it?
我有一个我希望访问的Postgres数据库。 我需要调用DB中存在的几个函数。 我已经实现了与数据库的连接和运行查询。 我也实现了调用我想要的功能。 我的问题是,当Postgres函数有多个OUT参数时,不是返回一个数组,而是可以使用偏移量或行名称访问它,而是返回一个包含OUT参数的字符串: p>
var_dumb返回的是: p>
我当然可以解析那个字符串但是有办法 实现我想要的,我想念它? p>
div> $ query =“选择pg_function('”。Getenv(“REMOTE_ADDR”)。“')”;
$ result = pg_query($ query);
$ line = pg_fetch_array($ result) ,NULL,PGSQL_ASSOC);
var_dump($ line);
code> pre>
array
'ua_bl_session_request'=> string'(c6787f5f2b885168162c8c8ffff220,8fe04393-c188-0e08-b710-a2ce89066768)'(length = 71)
code> pre>
SELECT * FROM pg_function(REMOTE_ADDR);
Maybe you are using pg_fetch_array() incorrectly, because you should give the row number in the second parameter. For example:
// To get the first row (Rows are numbered from 0 upwards)
$line = pg_fetch_array($result, 0, PGSQL_ASSOC);
// To get the second row
$line = pg_fetch_array($result, 1, PGSQL_ASSOC);
But if you know what you are doing, you can use regexp_split_to_table(), it is a Posgtres string function that split a string to rows using a POSIX regular expression as the delimiter. For example:
$query = "select regexp_split_to_table(
pg_function('" . Getenv("REMOTE_ADDR") . "'), ','
)";
In your case, it will split the function's result using ','
as delimiter. It should return a result set with two rows, like below:
c6787f5f2b2c885168162c8c8ffff220
8fe04393-c188-0e08-b710-a2ce89066768