Skip to content

Instantly share code, notes, and snippets.

@mikedamage
Created January 15, 2009 23:06
Show Gist options
  • Save mikedamage/47688 to your computer and use it in GitHub Desktop.
Save mikedamage/47688 to your computer and use it in GitHub Desktop.
$result = mysql_query("SELECT * FROM table ORDER BY column DESC");
// 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
while ($row = mysql_fetch_assoc($result))
{
$fields .= '"'.$row['id'].'","'.$row['name'].'","'.$row['email'].'","'.$row['registration_date'].'"'."\n";
}
// Set our headers
header('Content-type: text/csv');
// To display data in-browser, change the header below to:
// header("Content-Disposition: inline");
header("Content-Disposition: attachment; filename=event_registrations.csv");
// Output our data
echo $fields;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment