pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andy Shellam (Mailing Lists) <andy.shellam-lists@mailnetwork.co.uk>
To: Jean-Christophe Roux <jcxxr@yahoo.com>
To: pgsql-php@postgresql.org
Subject: Re: function with array parameter
Date: Wed, 08 Nov 2006 21:37:22 +0000
Message-ID: <45524E12.30307@mailnetwork.co.uk> (raw)
In-Reply-To: <20061108213022.62179.qmail@web35301.mail.mud.yahoo.com>
References: <20061108213022.62179.qmail@web35301.mail.mud.yahoo.com>

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! 




view thread (7+ messages)  latest in thread

Message-ID: <45524E12.30307@mailnetwork.co.uk>
Permalink:  ../45524E12.30307@mailnetwork.co.uk/
Also on:    postgresql.org/message-id/45524E12.30307@mailnetwork.co.uk

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: andy.shellam-lists@mailnetwork.co.uk, jcxxr@yahoo.com
  Subject: Re: function with array parameter
  In-Reply-To: <45524E12.30307@mailnetwork.co.uk>
  References: <20061108213022.62179.qmail@web35301.mail.mud.yahoo.com> <45524E12.30307@mailnetwork.co.uk>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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