0

I am using php to export data from Database.My problem is.Data export successfully but it create two blank rows at beginning.I want to remove them.Here is my code

if($_POST){
$output = "";
$table = "schedule"; // Enter Your Table Name 
$sql = mysql_query("select * from $table WHERE post_id='".$_POST['schedule_id']."'");
$columns_total = mysql_num_fields($sql);
for ($i = 0; $i < $columns_total; $i++) {
$heading = mysql_field_name($sql, $i);
$output .= '"'.$heading.'",';
}
$output .="\n";
while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.trim($row["$i"]).'",';
}
$output .="\n";
}
$filename = "schedule.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
echo $output;
exit;
}
  • possible duplicate of [Convert array into csv](http://stackoverflow.com/questions/3933668/convert-array-into-csv) – Maks3w Apr 19 '14 at 09:27
  • Consider using PHP's built-in [fputcsv()](http://www.php.net/manual/en/function.fputcsv.php) function to create a CSV File rather than badly re-inventing the wheel – Mark Baker Apr 19 '14 at 09:41

1 Answers1

1

You should wrap string with spaces with double quotes or any other character.

See the function provided by this answer https://stackoverflow.com/a/3933816/1163444

Community
  • 1
  • 1
Maks3w
  • 6,014
  • 6
  • 37
  • 42