logo       

Re: function with array parameter: msg#00023

Subject: Re: function with array parameter
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@xxxxxxxxxxxxxxxxx>
To: Jean-Christophe Roux <jcxxr@xxxxxxxxx>; pgsql-php@xxxxxxxxxxxxxx
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@xxxxxxxxx>
> To: Jean-Christophe Roux <jcxxr@xxxxxxxxx>
> Cc: pgsql-php@xxxxxxxxxxxxxx
> 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@xxxxxxxxx
> <mailto:jcxxr@xxxxxxxxx>> 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


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
audio.irate.dev...    yellowdog.gener...    ietf.ips/2002-0...    xfree86.fonts/2...    busybox/2003-07...    emacs.jdee/2004...    linux.mandrake....    hardware.microc...    user-groups.lin...    science.analysi...    version-control...    db.filemaker.de...    cluster.openmos...    mail.eyebrowse....    text.xml.xerces...    kde.devel.kwrit...    finance.moneyda...    gcc.regression/...    network.routing...    os.freebsd.deve...    recreation.radi...    qnx.openqnx.dev...    python.xml/2002...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe