Dave, et al,
My client wants to have the ability to save the mySQL database data to a MS .csv file format (for import to an Excel worksheet). I found the following script (and reworked it a little) that works fine. It even appends the date and time to the file name (very cool). Problem: I'm not sue how to specify what fields to save (as opposed to all fields). They only want a few of the fields to review. Any suggestions? [unsure]
<?
$host = '<your_host>';
$user = '<your_user>';
$pass = '<password>';
$db = '<db_name>';
$table = '<table_name>';
$file = 'output_file_name';
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$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 * FROM ".$table."");
$values = mysql_query("SELECT * FROM $table WHERE paid = '1'");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i-1;$j++) {
$csv_output .=$rowr[$j].",";
}
$csv_output .=$rowr[$j].",";
$csv_output .= "\n";
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>
Thanks!
Eric