pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
function with array parameter
7+ messages / 4 participants
[nested] [flat]

* function with array parameter
@ 2006-11-08 20:07 Jean-Christophe Roux <jcxxr@yahoo.com>
  2006-11-08 20:25 ` Re: function with array parameter Talha Khan <talha.amjad@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Jean-Christophe Roux @ 2006-11-08 20:07 UTC (permalink / raw)
  To: pgsql-php

Hello,
I have a function that I would like to call from a php script:

CREATE OR REPLACE FUNCTION a_dummy(arr_in text[])
  RETURNS text AS
$BODY$
declare
begin
    return arr_in[1];
end;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE;

and the php code would be something like that
$arr;
$arr[0] = "one";
$arr[1] = 'two';
$query = "select func_a_dummy($arr)";
$result = pg_query($query);
echo pg_fetch_result($result, 0, 0);


but the syntax is wrong. Any idea what I should do to make it work
Thank you





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: function with array parameter
  2006-11-08 20:07 function with array parameter Jean-Christophe Roux <jcxxr@yahoo.com>
@ 2006-11-08 20:25 ` Talha Khan <talha.amjad@gmail.com>
  2006-11-08 20:29   ` Re: function with array parameter Alan Hodgson <ahodgson@simkin.ca>
  0 siblings, 1 reply; 7+ messages in thread

From: Talha Khan @ 2006-11-08 20:25 UTC (permalink / raw)
  To: Jean-Christophe Roux <jcxxr@yahoo.com>; +Cc: pgsql-php

Hi Jean,

>>$result = pg_query($query);

try it as follows

$result=pg_query($database,$query);

where

$database=<database name>

Regards,
Talha Khan

On 11/9/06, Jean-Christophe Roux <jcxxr@yahoo.com> wrote:
>
> Hello,
> I have a function that I would like to call from a php script:
>
> CREATE OR REPLACE FUNCTION a_dummy(arr_in text[])
>   RETURNS text AS
> $BODY$
> declare
> begin
>     return arr_in[1];
> end;
> $BODY$
>   LANGUAGE 'plpgsql' VOLATILE;
>
> and the php code would be something like that
> $arr;
> $arr[0] = "one";
> $arr[1] = 'two';
> $query = "select func_a_dummy($arr)";
> $result = pg_query($query);
> echo pg_fetch_result($result, 0, 0);
>
>
> but the syntax is wrong. Any idea what I should do to make it work
> Thank you
>
>

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: function with array parameter
  2006-11-08 20:07 function with array parameter Jean-Christophe Roux <jcxxr@yahoo.com>
  2006-11-08 20:25 ` Re: function with array parameter Talha Khan <talha.amjad@gmail.com>
@ 2006-11-08 20:29   ` Alan Hodgson <ahodgson@simkin.ca>
  0 siblings, 0 replies; 7+ messages in thread

From: Alan Hodgson @ 2006-11-08 20:29 UTC (permalink / raw)
  To: pgsql-php

> On 11/9/06, Jean-Christophe Roux <jcxxr@yahoo.com> wrote:
> > Hello,
> > I have a function that I would like to call from a php script:
> >
> > CREATE OR REPLACE FUNCTION a_dummy(arr_in text[])
> >   RETURNS text AS
> > $BODY$
> > declare
> > begin
> >     return arr_in[1];
> > end;
> > $BODY$
> >   LANGUAGE 'plpgsql' VOLATILE;
> >
> > and the php code would be something like that
> > $arr;
> > $arr[0] = "one";
> > $arr[1] = 'two';
> > $query = "select func_a_dummy($arr)";
> > $result = pg_query($query);
> > echo pg_fetch_result($result, 0, 0);


A PHP array doesn't translate to a PostgreSQL array.  You have to build up a 
string to pass to the function ( ie. '{"value1","value2"}' ) .

-- 
"Emacs is great.  But hang onto vim, because you'll still need a decent
text editor." - seen on /.




^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: function with array parameter
@ 2006-11-08 21:30 Jean-Christophe Roux <jcxxr@yahoo.com>
  2006-11-08 21:37 ` Re: function with array parameter Andy Shellam (Mailing Lists) <andy.shellam-lists@mailnetwork.co.uk>
  0 siblings, 1 reply; 7+ messages in thread

From: Jean-Christophe Roux @ 2006-11-08 21:30 UTC (permalink / raw)
  To: Talha Khan <talha.amjad@gmail.com>; +Cc: pgsql-php

Hello,

Thanks for the answer but the resource connection is optional and in this script there is no ambiguity since there is only one connection. I added the connection to pg_query any way and the script still fails. I went through archives and saw that others had the same problem with using a PHP arrays as a parameter to a Postgresql function. I have not found a solution though; any thought?

----- Original Message ----
From: Talha Khan <talha.amjad@gmail.com>
To: Jean-Christophe Roux <jcxxr@yahoo.com>
Cc: pgsql-php@postgresql.org
Sent: Wednesday, November 8, 2006 3:25:29 PM
Subject: Re: [PHP] function with array parameter

Hi Jean,

>>$result = pg_query($query);

try it as follows

$result=pg_query($database,$query);

where

$database=<database name>

Regards,

Talha Khan

On 11/9/06, Jean-Christophe Roux <jcxxr@yahoo.com> wrote:
Hello,
I have a function that I would like to call from a php script:

CREATE OR REPLACE FUNCTION a_dummy(arr_in text[])

  RETURNS text AS
$BODY$
declare
begin
    return arr_in[1];
end;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE;

and the php code would be something like that
$arr;
$arr[0] = "one";
$arr[1] = 'two';

$query = "select func_a_dummy($arr)";
$result = pg_query($query);
echo pg_fetch_result($result, 0, 0);


but the syntax is wrong. Any idea what I should do to make it work
Thank you
















^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: function with array parameter
  2006-11-08 21:30 Re: function with array parameter Jean-Christophe Roux <jcxxr@yahoo.com>
@ 2006-11-08 21:37 ` Andy Shellam (Mailing Lists) <andy.shellam-lists@mailnetwork.co.uk>
  0 siblings, 0 replies; 7+ messages in thread

From: Andy Shellam (Mailing Lists) @ 2006-11-08 21:37 UTC (permalink / raw)
  To: Jean-Christophe Roux <jcxxr@yahoo.com>; pgsql-php

You could do something like:

(where $source_array is your array to pass to the PGSQL function):

<?php
// start off the query string
$query = "select func_a_dummy(";

// for each element of the array add to the string
foreach ($source_array AS $func_parameter)
{
    $query .= "\"" . $func_parameter . "\", ";
}

// we'll end up with a trailing ", " on the end of the string due to the 
final parameter, so remove it
$query = substring($query, 0, strlen($query) - 2);

// and close of the query string
$query .= ");";
?>

Then send $query to the database.  That will create you something like 
(if $source_array contains "param1" and "param2"):

select func_a_dummy("param1", "param2");

Regards,

Andy.

Jean-Christophe Roux wrote:
> Hello,
>
> Thanks for the answer but the resource connection is optional and in 
> this script there is no ambiguity since there is only one connection. 
> I added the connection to pg_query any way and the script still fails. 
> I went through archives and saw that others had the same problem with 
> using a PHP arrays as a parameter to a Postgresql function. I have not 
> found a solution though; any thought?
>
> ----- Original Message ----
> From: Talha Khan <talha.amjad@gmail.com>
> To: Jean-Christophe Roux <jcxxr@yahoo.com>
> Cc: pgsql-php@postgresql.org
> Sent: Wednesday, November 8, 2006 3:25:29 PM
> Subject: Re: [PHP] function with array parameter
>
> Hi Jean,
>
> >>$result = pg_query($query);
>
> try it as follows
>
> $result=pg_query($database,$query);
>
> where
>
> $database=<database name>
>
> Regards,
> Talha Khan
>
> On 11/9/06, *Jean-Christophe Roux* <jcxxr@yahoo.com 
> <mailto:jcxxr@yahoo.com>> wrote:
>
>     Hello,
>     I have a function that I would like to call from a php script:
>
>     CREATE OR REPLACE FUNCTION a_dummy(arr_in text[])
>       RETURNS text AS
>     $BODY$
>     declare
>     begin
>         return arr_in[1];
>     end;
>     $BODY$
>       LANGUAGE 'plpgsql' VOLATILE;
>
>     and the php code would be something like that
>     $arr;
>     $arr[0] = "one";
>     $arr[1] = 'two';
>     $query = "select func_a_dummy($arr)";
>     $result = pg_query($query);
>     echo pg_fetch_result($result, 0, 0);
>
>
>     but the syntax is wrong. Any idea what I should do to make it work
>     Thank you
>
>
>
>
> !DSPAM:37,45524c9640412067911618! 




^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: function with array parameter
@ 2006-11-08 21:52 Jean-Christophe Roux <jcxxr@yahoo.com>
  2006-11-08 21:54 ` Re: function with array parameter Andy Shellam (Mailing Lists) <andy.shellam-lists@mailnetwork.co.uk>
  0 siblings, 1 reply; 7+ messages in thread

From: Jean-Christophe Roux @ 2006-11-08 21:52 UTC (permalink / raw)
  To: andy.shellam-lists@mailnetwork.co.uk, Talha Khan <talha.amjad@gmail.com>; +Cc: pgsql-php

Hello,

Looks like it is best to forget about passing an array directly between php and postgresql and instead rely on some string.

thanks to all


----- Original Message ----
From: Andy Shellam (Mailing Lists) <andy.shellam-lists@mailnetwork.co.uk>
To: Jean-Christophe Roux <jcxxr@yahoo.com>; pgsql-php@postgresql.org
Sent: Wednesday, November 8, 2006 4:37:22 PM
Subject: Re: [PHP] function with array parameter

You could do something like:

(where $source_array is your array to pass to the PGSQL function):

<?php
// start off the query string
$query = "select func_a_dummy(";

// for each element of the array add to the string
foreach ($source_array AS $func_parameter)
{
    $query .= "\"" . $func_parameter . "\", ";
}

// we'll end up with a trailing ", " on the end of the string due to the 
final parameter, so remove it
$query = substring($query, 0, strlen($query) - 2);

// and close of the query string
$query .= ");";
?>

Then send $query to the database.  That will create you something like 
(if $source_array contains "param1" and "param2"):

select func_a_dummy("param1", "param2");

Regards,

Andy.

Jean-Christophe Roux wrote:
> Hello,
>
> Thanks for the answer but the resource connection is optional and in 
> this script there is no ambiguity since there is only one connection. 
> I added the connection to pg_query any way and the script still fails. 
> I went through archives and saw that others had the same problem with 
> using a PHP arrays as a parameter to a Postgresql function. I have not 
> found a solution though; any thought?
>
> ----- Original Message ----
> From: Talha Khan <talha.amjad@gmail.com>
> To: Jean-Christophe Roux <jcxxr@yahoo.com>
> Cc: pgsql-php@postgresql.org
> Sent: Wednesday, November 8, 2006 3:25:29 PM
> Subject: Re: [PHP] function with array parameter
>
> Hi Jean,
>
> >>$result = pg_query($query);
>
> try it as follows
>
> $result=pg_query($database,$query);
>
> where
>
> $database=<database name>
>
> Regards,
> Talha Khan
>
> On 11/9/06, *Jean-Christophe Roux* <jcxxr@yahoo.com 
> <mailto:jcxxr@yahoo.com>> wrote:
>
>     Hello,
>     I have a function that I would like to call from a php script:
>
>     CREATE OR REPLACE FUNCTION a_dummy(arr_in text[])
>       RETURNS text AS
>     $BODY$
>     declare
>     begin
>         return arr_in[1];
>     end;
>     $BODY$
>       LANGUAGE 'plpgsql' VOLATILE;
>
>     and the php code would be something like that
>     $arr;
>     $arr[0] = "one";
>     $arr[1] = 'two';
>     $query = "select func_a_dummy($arr)";
>     $result = pg_query($query);
>     echo pg_fetch_result($result, 0, 0);
>
>
>     but the syntax is wrong. Any idea what I should do to make it work
>     Thank you
>
>
>
>
> !DSPAM:37,45524c9640412067911618! 


---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster








^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: function with array parameter
  2006-11-08 21:52 Re: function with array parameter Jean-Christophe Roux <jcxxr@yahoo.com>
@ 2006-11-08 21:54 ` Andy Shellam (Mailing Lists) <andy.shellam-lists@mailnetwork.co.uk>
  0 siblings, 0 replies; 7+ messages in thread

From: Andy Shellam (Mailing Lists) @ 2006-11-08 21:54 UTC (permalink / raw)
  To: Jean-Christophe Roux <jcxxr@yahoo.com>; +Cc: pgsql-php

Yes - PHP arrays will inevitably be different to PostgreSQL arrays.

Jean-Christophe Roux wrote:
> Hello,
>
> Looks like it is best to forget about passing an array directly 
> between php and postgresql and instead rely on some string.
>
> thanks to all
>
>
> ----- Original Message ----
> From: Andy Shellam (Mailing Lists) <andy.shellam-lists@mailnetwork.co.uk>
> To: Jean-Christophe Roux <jcxxr@yahoo.com>; pgsql-php@postgresql.org
> Sent: Wednesday, November 8, 2006 4:37:22 PM
> Subject: Re: [PHP] function with array parameter
>
> You could do something like:
>
> (where $source_array is your array to pass to the PGSQL function):
>
> <?php
> // start off the query string
> $query = "select func_a_dummy(";
>
> // for each element of the array add to the string
> foreach ($source_array AS $func_parameter)
> {
>     $query .= "\"" . $func_parameter . "\", ";
> }
>
> // we'll end up with a trailing ", " on the end of the string due to the
> final parameter, so remove it
> $query = substring($query, 0, strlen($query) - 2);
>
> // and close of the query string
> $query .= ");";
> ?>
>
> Then send $query to the database.  That will create you something like
> (if $source_array contains "param1" and "param2"):
>
> select func_a_dummy("param1", "param2");
>
> Regards,
>
> Andy.
>
> Jean-Christophe Roux wrote:
> > Hello,
> >
> > Thanks for the answer but the resource connection is optional and in
> > this script there is no ambiguity since there is only one connection.
> > I added the connection to pg_query any way and the script still fails.
> > I went through archives and saw that others had the same problem with
> > using a PHP arrays as a parameter to a Postgresql function. I have not
> > found a solution though; any thought?
> >
> > ----- Original Message ----
> > From: Talha Khan <talha.amjad@gmail.com>
> > To: Jean-Christophe Roux <jcxxr@yahoo.com>
> > Cc: pgsql-php@postgresql.org
> > Sent: Wednesday, November 8, 2006 3:25:29 PM
> > Subject: Re: [PHP] function with array parameter
> >
> > Hi Jean,
> >
> > >>$result = pg_query($query);
> >
> > try it as follows
> >
> > $result=pg_query($database,$query);
> >
> > where
> >
> > $database=<database name>
> >
> > Regards,
> > Talha Khan
> >
> > On 11/9/06, *Jean-Christophe Roux* <jcxxr@yahoo.com
> > <mailto:jcxxr@yahoo.com>> wrote:
> >
> >     Hello,
> >     I have a function that I would like to call from a php script:
> >
> >     CREATE OR REPLACE FUNCTION a_dummy(arr_in text[])
> >       RETURNS text AS
> >     $BODY$
> >     declare
> >     begin
> >         return arr_in[1];
> >     end;
> >     $BODY$
> >       LANGUAGE 'plpgsql' VOLATILE;
> >
> >     and the php code would be something like that
> >     $arr;
> >     $arr[0] = "one";
> >     $arr[1] = 'two';
> >     $query = "select func_a_dummy($arr)";
> >     $result = pg_query($query);
> >     echo pg_fetch_result($result, 0, 0);
> >
> >
> >     but the syntax is wrong. Any idea what I should do to make it work
> >     Thank you
> >
> >
> >
> >
> >
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster
>
>
> !DSPAM:37,455251b340411983042122! 




^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2006-11-08 21:54 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-11-08 20:07 function with array parameter Jean-Christophe Roux <jcxxr@yahoo.com>
2006-11-08 20:25 ` Talha Khan <talha.amjad@gmail.com>
2006-11-08 20:29   ` Alan Hodgson <ahodgson@simkin.ca>
2006-11-08 21:30 Re: function with array parameter Jean-Christophe Roux <jcxxr@yahoo.com>
2006-11-08 21:37 ` Andy Shellam (Mailing Lists) <andy.shellam-lists@mailnetwork.co.uk>
2006-11-08 21:52 Re: function with array parameter Jean-Christophe Roux <jcxxr@yahoo.com>
2006-11-08 21:54 ` Andy Shellam (Mailing Lists) <andy.shellam-lists@mailnetwork.co.uk>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox