views:

26

answers:

2

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 (http://stackoverflow.com/questions/3068683/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  A: 

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

Marc B
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.
Bart Gottschalk
Marc, Scott provided an even better answer below. Thanks again for your assistance on this.
Bart Gottschalk
+1  A: 

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.

Scott Bailey
Perfect. Thanks Scott.
Bart Gottschalk