I am using this code to export selected data from MySQL to CSV using PHP, I am facing a little problem that when data exported to the csv file it looks like below:
First line in its correct place, but starting from the second row data shifts to the right one space, if I open the csv file in Excel, I can see the most left cell after the first row is empty.
parts destination
===================
1 9.71504E+11
1 9.71504E+11
1 96656587662
1 9.71504E+11
This is my code :
$values =mysql_query( "SELECT parts,destination from log");
$rown = 0;
$row = array();
$row[] = 'parts';
$row[] = 'destination'."\n";
$data .= join(',', $row)."\n";
while( $row_select = mysql_fetch_assoc($values) ) {
if($rown++==0)
$row = array();
$row[] = $row_select['parts'];
$row[] = $row_select['destination']."\n";
}
$data .= join(',', $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 $data;
exit();
Would you please help?
Regards,