Pages

Wednesday, March 13, 2013

mysql to csv export using php

mysql to csv export using php

The following PHP code is intendend to be used to export a MySQL table in CSV format in
order to be used with MS Excel.


$file="report";
$i=1;
$values = mysql_query("SELECT * FROM report");
$rown=0;
while( $row = mysql_fetch_assoc($values))
{
if($rown++==0)
$csv_output.=implode(";",array_keys($row))."\n";
$csv_output.=implode(";",array_values($row))."\n";
}

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


No comments:

Post a Comment