Re: where is the output

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: ohp(at)pyrenet(dot)fr
Cc: pgsql-hackers list <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: where is the output
Date: 2005-12-21 20:23:24
Message-ID: 20051221202324.GA59151@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Dec 21, 2005 at 07:16:28PM +0100, ohp(at)pyrenet(dot)fr wrote:
> Not sure it's the right group, but I've spent the afternoon googling and
> trying on this.
>
> In PHP (Apache Module)

pgsql-php might be more appropriate, or possibly a PHP mailing list.

> I try pg_exec("COPY blah TO STDOUT WITH blah");
> It runs for ever...
>
> How can I get the output of COPY in PHP?

At the libpq layer you'd use PQgetCopyData(); the only place I see
a call to that function in the PHP 5.1.1 source code is in the
pg_copy_to() function, which is for copying a table into an array.

> Copy_from is not an option because the goal is to get a CSV file.
> Writing and re-reading a file doesn't seem to be an option either because
> of safe_mode.

Could you use pg_copy_to() to read the table into an array and write
each row of the array as CSV? I'm not sure if the following is the
best way or even entirely correct, but it appears to work in PHP 5.1.1,
even with safe_mode enabled:

$rows = pg_copy_to($conn, "tablename");
$fh = fopen("php://output", "w");
foreach ($rows as $row) {
fputcsv($fh, explode("\t", rtrim($row)));
}
fclose($fh);

According to the PHP documentation fputcsv() is new as of 5.1.0RC1;
for earlier versions you could probably find a module to generate
CSV output.

--
Michael Fuhr

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Eisentraut 2005-12-21 21:23:45 Re: horology regression test failure
Previous Message Jaime Casanova 2005-12-21 20:16:06 Re: [pgadmin-hackers] Client-side password encryption