From jcxxr@yahoo.com Sat Oct 14 20:28:50 2006 Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 740059FA0C0 for ; Sat, 14 Oct 2006 17:28:50 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 26003-09 for ; Sat, 14 Oct 2006 17:28:47 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from web35304.mail.mud.yahoo.com (web35304.mail.mud.yahoo.com [66.163.179.98]) by postgresql.org (Postfix) with SMTP id 06E3F9FB1FF for ; Sat, 14 Oct 2006 17:28:46 -0300 (ADT) Received: (qmail 17632 invoked by uid 60001); 14 Oct 2006 20:28:41 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:Received:Date:From:Subject:To:MIME-Version:Content-Type; b=NM7muPydcXgbrND9QQj4kH5AMSB68LEAJ37kQcVERlG+SnUh8pWwaXbXTNKyWipXwhJV53tGAHBTfMwDULfAK7d/0Ec+spzH7N3E4zNkjfYZtvdHPgIe/+owLDxj0O9MblatM2vJRAyGZIskkIlurDiR3pdQXHoNnjV1dHZX0X8= ; Message-ID: <20061014202841.17630.qmail@web35304.mail.mud.yahoo.com> Received: from [74.98.189.48] by web35304.mail.mud.yahoo.com via HTTP; Sat, 14 Oct 2006 13:28:41 PDT Date: Sat, 14 Oct 2006 13:28:41 -0700 (PDT) From: Jean-Christophe Roux Subject: plpgsql returning array To: pgsql-php@postgresql.org MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="0-455368344-1160857721=:15808" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=3.425 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, DNS_FROM_RFC_POST, DNS_FROM_RFC_WHOIS, HTML_40_50, HTML_MESSAGE X-Spam-Level: *** X-Archive-Number: 200610/14 X-Sequence-Number: 3614 --0-455368344-1160857721=:15808 Content-Type: text/plain; charset=ascii Content-Transfer-Encoding: quoted-printable Hello,=0Ain Postgresql, I have a function like this=0A=0ACREATE OR REPLACE = FUNCTION foo()=0A RETURNS text[] AS=0A$BODY$=0Adeclare=0A a text;=0A = b text;=0A arr text[];=0Abegin=0A a =3D 'a';=0A b =3D 'b';=0A = arr[0] =3D a;=0A arr[1] =3D b;=0A return arr;=0Aend;=0A$BODY$=0A LAN= GUAGE 'plpgsql' VOLATILE;=0A=0AIn PHP I would like to read my returned arr.= =0A=0Aprints something like:=0A[0= :1]=3D{a,b}=0A=0A=0AI could parse that results, as a string but I'd rather = have a nice array. Any idea how I should change the function and/or the php= codes? =0AThanks in advance=0AJean=0A=0A=0A=0A --0-455368344-1160857721=:15808 Content-Type: text/html; charset=ascii Content-Transfer-Encoding: quoted-printable
Hello,
in Postgresql, I have a function like this
CREATE OR REPLACE FUNCTION foo()
  RETURNS text[] AS
$BODY= $
declare
    a text;
    b text;    arr text[];
begin
    a =3D 'a';    b =3D 'b';
    arr[0] =3D a;
&nbs= p;   arr[1] =3D b;
    return arr;
end;
$= BODY$
  LANGUAGE 'plpgsql' VOLATILE;

In PHP I would like to = read my returned arr.
<?php
        =     $message =3D pg_fetch_result(pg_query("select foo()"),0,= 0);
            print_r($me= ssage);
?>
prints something like:
[0:1]=3D{a,b}


I co= uld parse that results, as a string but I'd rather have a nice array. Any idea how I should change the = function and/or the php codes?
Thanks in advance
Jean

--0-455368344-1160857721=:15808-- From mmcintyre@squiz.net Sun Oct 15 06:16:46 2006 Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 2C8629FB21D for ; Sun, 15 Oct 2006 03:16:46 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 22223-07 for ; Sun, 15 Oct 2006 03:16:43 -0300 (ADT) X-Greylist: delayed 00:05:04.760239 by SQLgrey- Received: from ipmail01.adl2.internode.on.net (ipmail01.adl2.internode.on.net [203.16.214.140]) by postgresql.org (Postfix) with ESMTP id 7187E9FAE4E for ; Sun, 15 Oct 2006 03:16:40 -0300 (ADT) Received: from ppp1-14.lns1.syd7.internode.on.net (HELO [192.168.0.4]) ([59.167.1.14]) by ipmail01.adl2.internode.on.net with ESMTP; 15 Oct 2006 15:41:32 +0930 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AQAAAC1sMUUNjQE X-IronPort-AV: i="4.09,312,1157293800"; d="scan'208"; a="29234364:sNHT33666073" Message-ID: <4531D112.90301@squiz.net> Date: Sun, 15 Oct 2006 16:11:30 +1000 From: Marc McIntyre User-Agent: Thunderbird 1.5.0.7 (Macintosh/20060909) MIME-Version: 1.0 To: Jean-Christophe Roux CC: pgsql-php@postgresql.org Subject: Re: plpgsql returning array References: <20061014202841.17630.qmail@web35304.mail.mud.yahoo.com> In-Reply-To: <20061014202841.17630.qmail@web35304.mail.mud.yahoo.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.383 tagged_above=0 required=5 tests=SPF_HELO_PASS, SPF_SOFTFAIL X-Spam-Level: * X-Archive-Number: 200610/15 X-Sequence-Number: 3615 You should use a set returning function. Your function can be rewritten as a set returning function like so: CREATE OR REPLACE FUNCTION foo() RETURNS SETOF text AS $BODY$ declare a text; b text; arr text[]; i INTEGER := 0; begin a = 'a'; b = 'b'; arr[0] = a; arr[1] = b; FOR i IN 0..array_upper(arr, 1) LOOP RETURN NEXT arr[i]; END LOOP; end; $BODY$ LANGUAGE 'plpgsql' VOLATILE; And to call the function: marc=# select * from foo(); foo ----- a b (2 rows) Notice that to return a value within the set you use the construct "RETURN NEXT". Set returning functions are generally faster than array returning functions, and are also more useful. For instance you can use set returning functions in sub selects like so: Select * from table where col in (select * from foo()) You can also use a where clause within the select statement ie. marc=# select * from foo() as x where x = 'a'; x --- a (1 row) So a set returning function is like a derived table. In php, the result from a set returning function would be the same as querying a table. Jean-Christophe Roux wrote: > Hello, > in Postgresql, I have a function like this > > CREATE OR REPLACE FUNCTION foo() > RETURNS text[] AS > $BODY$ > declare > a text; > b text; > arr text[]; > begin > a = 'a'; > b = 'b'; > arr[0] = a; > arr[1] = b; > return arr; > end; > $BODY$ > LANGUAGE 'plpgsql' VOLATILE; > > In PHP I would like to read my returned arr. > $message = pg_fetch_result(pg_query("select foo()"),0,0); > print_r($message); > ?> > prints something like: > [0:1]={a,b} > > > I could parse that results, as a string but I'd rather have a nice > array. Any idea how I should change the function and/or the php codes? > Thanks in advance > Jean > From jcxxr@yahoo.com Tue Oct 17 02:02:40 2006 Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id D258B9FB43A for ; Mon, 16 Oct 2006 23:02:40 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 66034-02 for ; Mon, 16 Oct 2006 23:02:35 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from web35311.mail.mud.yahoo.com (web35311.mail.mud.yahoo.com [66.163.179.105]) by postgresql.org (Postfix) with SMTP id 338329FA085 for ; Mon, 16 Oct 2006 23:02:35 -0300 (ADT) Received: (qmail 88474 invoked by uid 60001); 17 Oct 2006 02:02:33 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:Received:Date:From:Subject:To:Cc:MIME-Version:Content-Type; b=WplT+RqluwSJEjl2ZF5QAzrZEmD5asQ5HAI0u0lfwakd0z1Sk8U7Wvk6/MJWavxi25eC8Pg2+ldSjUln4VWAavx03qM8DeAhxQRgy3wO0srej8WqWFlzOHFtnmqRkyXRRRdTX9Vn1Rhg77xwc4HMX9O+udgK4zZmlixzHxrcNDg= ; Message-ID: <20061017020233.88472.qmail@web35311.mail.mud.yahoo.com> Received: from [74.98.189.48] by web35311.mail.mud.yahoo.com via HTTP; Mon, 16 Oct 2006 19:02:33 PDT Date: Mon, 16 Oct 2006 19:02:33 -0700 (PDT) From: Jean-Christophe Roux Subject: Re: plpgsql returning array To: Marc McIntyre Cc: pgsql-php@postgresql.org MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="0-571368672-1161050553=:85347" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=3.496 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, DNS_FROM_RFC_POST, DNS_FROM_RFC_WHOIS, HTML_40_50, HTML_MESSAGE X-Spam-Level: *** X-Archive-Number: 200610/16 X-Sequence-Number: 3616 --0-571368672-1161050553=:85347 Content-Type: text/plain; charset=ascii Content-Transfer-Encoding: quoted-printable Marc,=0AThanks for the tip. It does the job! This "return setof/next" is qu= ite new to me; I am going to spend some time to understand it and see how i= t can improve codes. =0A=0A----- Original Message ----=0AFrom: Marc McIntyr= e =0ATo: Jean-Christophe Roux =0ACc: = pgsql-php@postgresql.org=0ASent: Sunday, October 15, 2006 2:11:30 AM=0ASubj= ect: Re: [PHP] plpgsql returning array=0A=0A=0AYou should use a set returni= ng function. Your function can be rewritten =0Aas a set returning function = like so:=0A=0ACREATE OR REPLACE FUNCTION foo()=0A RETURNS SETOF text AS=0A= $BODY$=0Adeclare=0A a text;=0A b text;=0A arr text[];=0A i INTE= GER :=3D 0;=0Abegin=0A a =3D 'a';=0A b =3D 'b';=0A arr[0] =3D a;= =0A arr[1] =3D b;=0A =0A FOR i IN 0..array_upper(arr, 1) LOOP=0A = RETURN NEXT arr[i];=0A END LOOP;=0A=0Aend;=0A$BODY$=0A LANGUAGE 'p= lpgsql' VOLATILE;=0A=0A=0AAnd to call the function:=0A=0Amarc=3D# select * = from foo();=0Afoo=0A-----=0Aa=0Ab=0A(2 rows)=0A=0ANotice that to return a v= alue within the set you use the construct =0A"RETURN NEXT". Set returning f= unctions are generally faster than array =0Areturning functions, and are al= so more useful. For instance you can use =0Aset returning functions in sub = selects like so:=0A=0ASelect * from table where col in (select * from foo()= )=0A=0AYou can also use a where clause within the select statement ie.=0A= =0Amarc=3D# select * from foo() as x where x =3D 'a';=0Ax=0A---=0Aa=0A(1 ro= w)=0A=0ASo a set returning function is like a derived table.=0A=0AIn php, t= he result from a set returning function would be the same as =0Aquerying a = table.=0A=0AJean-Christophe Roux wrote:=0A> Hello,=0A> in Postgresql, I hav= e a function like this=0A>=0A> CREATE OR REPLACE FUNCTION foo()=0A> RETUR= NS text[] AS=0A> $BODY$=0A> declare=0A> a text;=0A> b text;=0A> = arr text[];=0A> begin=0A> a =3D 'a';=0A> b =3D 'b';=0A> arr[0]= =3D a;=0A> arr[1] =3D b;=0A> return arr;=0A> end;=0A> $BODY$=0A> = LANGUAGE 'plpgsql' VOLATILE;=0A>=0A> In PHP I would like to read my return= ed arr.=0A> $message =3D pg_fetch_result(pg_query("se= lect foo()"),0,0);=0A> print_r($message);=0A> ?>=0A> prints som= ething like:=0A> [0:1]=3D{a,b}=0A>=0A>=0A> I could parse that results, as a= string but I'd rather have a nice =0A> array. Any idea how I should change= the function and/or the php codes?=0A> Thanks in advance=0A> Jean=0A>=0A= =0A --0-571368672-1161050553=:85347 Content-Type: text/html; charset=ascii Content-Transfer-Encoding: quoted-printable
=0A
Marc,
=0A
Thanks for t= he tip. It does the job! This "return setof/next" is quite new to me; I am = going to spend some time to understand it and see how it can improve c= odes.
=0A
----- Original Message ----
From: Marc McIn= tyre <mmcintyre@squiz.net>
To: Jean-Christophe Roux <jcxxr@yaho= o.com>
Cc: pgsql-php@postgresql.org
Sent: Sunday, October 15, 2006= 2:11:30 AM
Subject: Re: [PHP] plpgsql returning array

=0A
Yo= u should use a set returning function. Your function can be rewritten
a= s a set returning function like so:

CREATE OR REPLACE FUNCTION foo()=
  RETURNS SETOF text AS
$BODY$
declare
  &= nbsp; a text;
    b text;
   =  arr text[];
    i INTEGER :=3D 0;
begin
=     a =3D 'a';
    b =3D 'b';    arr[0] =3D a;
    arr[1] = =3D b;
  
    FOR i IN 0..array_upper(= arr, 1) LOOP
        RETURN NEXT= arr[i];
    END LOOP;

end;
$BODY$
&nbs= p; LANGUAGE 'plpgsql' VOLATILE;


And to call the function:
marc=3D# select * from foo();
foo
-----
a
b
(2 rows)
Notice that to return a value within the set you use the construct "RETURN NEXT". Set returning functions are generally faster than array
returning function= s, and are also more useful. For instance you can use
set returning fun= ctions in sub selects like so:

Select * from table where col in (sel= ect * from foo())

You can also use a where clause within the select = statement ie.

marc=3D# select * from foo() as x where x =3D 'a';
= x
---
a
(1 row)

So a set returning function is like a deriv= ed table.

In php, the result from a set returning function would be = the same as
querying a table.

Jean-Christophe Roux wrote:
>= ; Hello,
> in Postgresql, I have a function like this
>
>= CREATE OR REPLACE FUNCTION foo()
>   RETURNS text[] AS
= > $BODY$
> declare
>     a text;
>=      b text;
>     arr text[]= ;
> begin
>     a =3D 'a';
> &nb= sp;   b =3D 'b';
>     arr[0] =3D a;
>  &nbs= p;  arr[1] =3D b;
>     return arr;
> = end;
> $BODY$
>   LANGUAGE 'plpgsql' VOLATILE;
>= ;
> In PHP I would like to read my returned arr.
> <?php
= >            = ; $message =3D pg_fetch_result(pg_query("select foo()"),0,0);
> =             print_r(= $message);
> ?>
> prints something like:
> [0:1]=3D{a,= b}
>
>
> I could parse that results, as a string but I'd = rather have a nice
> array. Any idea how I should change the functio= n and/or the php codes?
> Thanks in advance
> Jean
>


--0-571368672-1161050553=:85347-- From jim@nasby.net Wed Oct 18 22:45:04 2006 Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 7EAF49FB2E6 for ; Wed, 18 Oct 2006 19:45:04 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 28770-09 for ; Wed, 18 Oct 2006 22:44:58 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from noel.decibel.org (noel.decibel.org [67.100.216.10]) by postgresql.org (Postfix) with ESMTP id 6F7369FA1E2 for ; Wed, 18 Oct 2006 19:44:58 -0300 (ADT) Received: by noel.decibel.org (Postfix, from userid 1001) id 450CA56456; Wed, 18 Oct 2006 17:44:57 -0500 (CDT) Received: (hashcash-sendmail, from uid 1001); Wed, 18 Oct 2006 17:44:53 -0500 Date: Wed, 18 Oct 2006 17:44:53 -0500 From: "Jim C. Nasby" To: Marc McIntyre Cc: Jean-Christophe Roux , pgsql-php@postgresql.org Subject: Re: plpgsql returning array Message-ID: <20061018224453.GF56874@nasby.net> References: <20061014202841.17630.qmail@web35304.mail.mud.yahoo.com> <4531D112.90301@squiz.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4531D112.90301@squiz.net> X-Operating-System: FreeBSD 6.1-RELEASE-p10 amd64 X-Distributed: Join the Effort! http://www.distributed.net User-Agent: Mutt/1.5.13 (2006-08-11) X-Hashcash: 1:20:061018:mmcintyre@squiz.net::UnRVWaLne32zGtnJ:000000000000000000 0000000000000000000000003RQC X-Hashcash: 1:20:061018:jcxxr@yahoo.com::tdkQpwMQj8ejETJb:000Tll X-Hashcash: 1:20:061018:pgsql-php@postgresql.org::jHjpW5lISJyizlzJ:0000000000000 000000000000000000000000Bncc X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200610/17 X-Sequence-Number: 3617 On Sun, Oct 15, 2006 at 04:11:30PM +1000, Marc McIntyre wrote: > You should use a set returning function. Your function can be rewritten > as a set returning function like so: Isn't there a way to return an array? SRFs are great, but not always the best tool. BTW, neither function should be marked as VOLATILE... they're actually IMMUTABLE as written. -- Jim Nasby jim@nasby.net EnterpriseDB http://enterprisedb.com 512.569.9461 (cell) From mmcintyre@squiz.net Thu Oct 19 11:37:23 2006 Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id ECDD19FB20F for ; Thu, 19 Oct 2006 08:37:23 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 75255-09 for ; Thu, 19 Oct 2006 08:37:16 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from ipmail02.adl2.internode.on.net (ipmail02.adl2.internode.on.net [203.16.214.141]) by postgresql.org (Postfix) with ESMTP id A23179FB1FE for ; Thu, 19 Oct 2006 08:37:11 -0300 (ADT) Received: from ppp225-169.lns3.syd7.internode.on.net (HELO [192.168.0.4]) ([59.167.225.169]) by ipmail02.adl2.internode.on.net with ESMTP; 19 Oct 2006 21:07:08 +0930 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AQAAAIL/NkUN X-IronPort-AV: i="4.09,329,1157293800"; d="scan'208"; a="31759379:sNHT95026862" Message-ID: <45376362.40804@squiz.net> Date: Thu, 19 Oct 2006 21:37:06 +1000 From: Marc McIntyre User-Agent: Thunderbird 1.5.0.7 (Macintosh/20060909) MIME-Version: 1.0 To: "Jim C. Nasby" CC: Jean-Christophe Roux , pgsql-php@postgresql.org Subject: Re: plpgsql returning array References: <20061014202841.17630.qmail@web35304.mail.mud.yahoo.com> <4531D112.90301@squiz.net> <20061018224453.GF56874@nasby.net> In-Reply-To: <20061018224453.GF56874@nasby.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.383 tagged_above=0 required=5 tests=SPF_HELO_PASS, SPF_SOFTFAIL X-Spam-Level: * X-Archive-Number: 200610/18 X-Sequence-Number: 3618 Jim C. Nasby wrote: > Isn't there a way to return an array? SRFs are great, but not always the > best tool. > > There's no way to nicely map an array from a plpgsql SF to a php array. (Ie. the driver won't do that for you, AFAIK) I find that array returning functions are useful when calling from other functions, and set returning functions are great when you wish to get a result set back to php.