2012-07-08 16:13 阅读 50


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);

What var_dumb returns is this:

    '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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    duanshan1511 duanshan1511 2012-07-09 06:25
    SELECT * FROM pg_function(REMOTE_ADDR);
    点赞 评论 复制链接分享
  • douguai7291 douguai7291 2012-07-08 18:02

    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:

    点赞 评论 复制链接分享