I am trying to export some some data from Mysql with PHP to .CSV file
The issue is script export .CSV file but in the .CSV file before tabels there are HTML code
Here you have the script
// Get all fields names in table "mytablename" in database "pendejas".
$fields = mysql_list_fields(pendejas,$table_name);
// Count the table fields and put the value into $columns.
$columns = mysql_num_fields($fields);
// Put the name of all fields to $out.
for ($i = 0; $i < $columns; $i++) {
$l=mysql_field_name($fields, $i);
$out .= '"'.$l.'",';
}
$out .="\n";
// Add all values in the table to $out.
while ($l = mysql_fetch_array($result)) {
for ($i = 0; $i < $columns; $i++) {
$out .='"'.$l["$i"].'",';
}
$out .="\n";
}
// Open file export.csv.
$f = fopen ('export.csv','w');
// Put all values from $out to export.csv.
fputs($f, $out);
fclose($f);
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename=$table_name.csv');
readfile('$table_name.csv');
?>
How can i export ONLY Mysql Table contains ?
appreciate your help
EDIT : SOLVED !
Here you have script
<?php
$result = mysql_query("SELECT name, time FROM $table_name");
ob_end_clean();
if ($result) {
while ($row = mysql_fetch_array($result)) {
$pasajeros .= $row["name"] . ";". $row["time"]. "\r\n"; //note the comma here
}
}
$filename = "pasajeros_" . date("Y-m-d_H-i");
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=" . $table_name . ".csv");
print $pasajeros;
exit();
?>