0

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?

1
  • var_dumb :-) I like that alias to be incorporated in core. Commented Jul 8, 2012 at 16:25

2 Answers 2

1
SELECT * FROM pg_function(REMOTE_ADDR);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.