/ Published in: PHP
# Export MySQL Data as CSV
This code takes a `mysql_query()` resource and outputs its rows into CSV spreadsheet format. Edit the `header('Content-Disposition...` declaration to tell the user's browser to either display the data as plain text or download it as an `attachment`.
__Note:__ This doesn't include the code to connect to the database in the first place. Don't forget that, otherwise this code is pretty useless.
This code takes a `mysql_query()` resource and outputs its rows into CSV spreadsheet format. Edit the `header('Content-Disposition...` declaration to tell the user's browser to either display the data as plain text or download it as an `attachment`.
__Note:__ This doesn't include the code to connect to the database in the first place. Don't forget that, otherwise this code is pretty useless.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// I hard-code the column names so I can capitalize, add spaces, etc. $fields = '"User ID","Name","Email","Registration Date"'."\n"; // Iterate through the rows of data { $fields .= '"'.$row['id'].'","'.$row['name'].'","'.$row['email'].'","'.$row['registration_date'].'"'."\n"; } // Set our headers // To display data in-browser, change the header below to: // header("Content-Disposition: inline"); // Output our data echo $fields;