Last active
September 4, 2022 03:24
-
-
Save rmontagud/1074382 to your computer and use it in GitHub Desktop.
Export a MySQL dataset to CSV using php://output stream and fputcsv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header('Content-Type: text/csv; charset=utf-8'); | |
header('Content-Disposition: attachment;filename="export.csv"'); | |
header('Cache-Control: max-age=0'); | |
// See: http://es2.php.net/manual/en/wrappers.php.php | |
$fpcsv = fopen('php://output', "a+"); | |
// Put your SQL query here | |
$exportcsv_q = mysql_query($query); | |
if (@mysql_num_rows($exportcsv_q) > 0) { | |
$campos = mysql_num_fields($exportcsv_q); | |
while ($exportcsv_r = mysql_fetch_row($exportcsv_q)) { | |
fputcsv($fpcsv, $exportcsv_r); | |
} | |
} | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment