Reputation: 9
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;
}
Upvotes: 0
Views: 6859
Reputation: 6429
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
Upvotes: 1