Page 1 of 1

Export Query results to CSV

Posted: Thu Apr 03, 2008 2:51 am
by new2phpcode
Hi is there anyone who has made a script to export query results from postgres sql to CSV , with a button that says download then a save as window box will appear so they can choose where the file would be save locally? im not pretty sure how this is called.... :?: Thanks!

Re: Export Query results to CSV

Posted: Thu Apr 03, 2008 3:15 am
by Christopher
Usually something like this will work:

Code: Select all

echo implode("\t", $row) . "\n";
//or 
echo '"' . implode('","', $row) . "\"\n";
Set the right header() and then loop though the results and echo the rows.

Re: Export Query results to CSV

Posted: Thu Apr 03, 2008 3:51 am
by new2phpcode
im not pretty sure how to do this, but its worth to try.. is there a working sample? :D thanks!

Re: Export Query results to CSV

Posted: Thu Apr 03, 2008 1:31 pm
by Christopher
The PHP manual is usually a good place to start. Check the pages for header() and implode() for examples.

Re: Export Query results to CSV

Posted: Sun Aug 03, 2008 11:34 pm
by new2phpcode
thanks aborint for the help! i got the code!

Code: Select all

 
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;