0

I have a postgresql (V 8.4) function that returns SETOF a custom type. An example of the string output from this function is the following 4 rows:

(2,"CD,100"," ","2010-09-08 14:07:59",New,0,,,,,,"2010-09-06 16:51:51","2010-09-07 16:51:57",)
(5,CD101,asdf,"2010-08-08 14:12:00",Suspended-Screen,1,10000,,,,,,,)
(4,DNR100,asdf,"2010-09-08 14:10:31",Suspended-Investgate,0,,,,,,"2010-09-06 16:51:51","2010-09-07 16:51:57",)
(3,MNSCU100," ","2010-09-08 14:09:07",Active,0,,,,,,,,)

I need to work with this data in PHP and I'm trying to figure out the best way to work with it. What I would love is if there was a way for postgresql to return this like a table where columns represent each value within a record rather than as a comma-separated string.

Is this possible? If not, what is the best way to work with this comma-separated string of values in PHP?

I've see this post (Convert PostgreSQL array to PHP array) and can use the function mentioned there but I wanted to ask if anyone has other ideas or suggestions.

Thanks, Bart

2 Answers 2

2

There's str_getcsv() which'll parse a string as CSV data and return an array of the individual fields

Sign up to request clarification or add additional context in comments.

2 Comments

Marc, thanks for your quick response. That seems to do what I need. I'm going to go forward with this option unless I come across something better. Thanks again.
Marc, Scott provided an even better answer below. Thanks again for your assistance on this.
1

Yep, its real easy, just change the way you are calling the function.

Instead of

SELECT my_srf(parm1);

Do either:

SELECT * FROM my_srf(parm1);
SELECT (my_srf(parm1)).*;

You'll even get the column names out this way.

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.