I want to download a MySQL table as CSV file but I am getting this error "Allowed memory size of 1342177280 bytes exhausted". I guess that is because the file is first created and then downloaded. How can I achive that the user can start downloading the file from 0, in this case I think that I wouldn't need so much memory.
This is my code so far:
$output = "";
$table = "export_table";
$sql = mysql_query("select * from $table");
$columns_total = mysql_num_fields($sql);
// Get The Field Name
for ($i = 0; $i < $columns_total; $i++) {
$heading = mysql_field_name($sql, $i);
$output .= '"'.$heading.'",';
}
$output .="\n";
// Get Records from the table
while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$row["$i"].'",';
}
$output .="\n";
}
// Download the file
$filename = "export.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
echo $output;
MYSQL SELECT INTO OUTFILEon SO and send me a drink, because you will really be that happy after using it compared to your own method :)