Skip to content

Instantly share code, notes, and snippets.

@ertankayalar
Created June 22, 2013 13:53
Show Gist options
  • Save ertankayalar/5840946 to your computer and use it in GitHub Desktop.
Save ertankayalar/5840946 to your computer and use it in GitHub Desktop.
create .csv from DB
<?php
import_request_variables('gpc');
$host = ''; // <-- db address
$user = ''; // <-- db user name
$pass = ''; // <-- password
$db = 'rsvp_list'; // db's name
$table = 'guest_list'; // table you want to export
$file = '6am_event_guest_list'; // csv name.
$link = mysql_connect($host, $user, $pass) or die("Cannot connect." . mysql_error());
mysql_select_db($db) or die("Cannot connect.");
$result = mysql_query("SHOW COLUMNS FROM ".$table." WHERE Field IN ('firstname', 'lastname', 'addons', 'email', 'rsvptime', 'eventname');");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].", ";
$i++;}
}
$csv_output .= "\n";
$values = mysql_query("SELECT eventname, firstname, lastname, addons, email, rsvptime FROM ".$table." WHERE eventname='$eventname'");
while ($rowr = mysql_fetch_row($values)) {
$rowr=str_replace(",","",(array)$rowr); // stripping all commas from the returned records so the cells are populated properly
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].", "; // adding comma only after the record to separate into it's own xls cells
}
$csv_output .= "\n";
}
$filename = $file."_".date("d-m-Y_H-i",time());
header("Content-type: text/csv");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment