mcmuney Posted May 14, 2010 Share Posted May 14, 2010 I'm using the code below to download DB results into a CSV file. Problem: In Excel, I have to perform the Delimited function to extract all the data from a single column to it's appropriate columns. I've see cases where this is done automatically. How can I change the code below to accommodate? $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.""); while ($rowr = mysql_fetch_row($values)) { for ($j=0;$j<$i;$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; Link to comment https://forums.phpfreaks.com/topic/201803-download-of-data-to-a-csv-file/ Share on other sites More sharing options...
litebearer Posted May 15, 2010 Share Posted May 15, 2010 Not sure if this helps, but it (A) puts field names in top row, then all data into appropriate columns. It uses comma as delim. http://www.electrictoolbox.com/create-csv-file-mysql-php/ Link to comment https://forums.phpfreaks.com/topic/201803-download-of-data-to-a-csv-file/#findComment-1058610 Share on other sites More sharing options...
kenrbnsn Posted May 15, 2010 Share Posted May 15, 2010 You might want to look at the fputcsv function which allows you to specify the string enclosure character. Ken Link to comment https://forums.phpfreaks.com/topic/201803-download-of-data-to-a-csv-file/#findComment-1058613 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.