tqla Posted April 25, 2008 Share Posted April 25, 2008 This script works but it has one little snag: Carriage returns in text fields are forcing text on new lines into new fields on the CSV doc. Does anybody know why? Thanks. <?php $host = ''; $user = ''; $pass = ''; $db = ''; $table = ''; $file = 'export'; $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.""); 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/102940-solved-export-db-to-csv-script-help/ Share on other sites More sharing options...
dptr1988 Posted April 25, 2008 Share Posted April 25, 2008 You could try using PHP's fputcsv, instead of writing your own function. http://us.php.net/fputcsv Here is example from the comments the fputcsv page on how you can use fputcsv to format your data. <?php // output up to 5MB is kept in memory, if it becomes bigger it will automatically be written to a temporary file $csv = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+'); fputcsv($csv, array('blah','blah')); rewind($csv); // put it all in a variable $output = stream_get_contents($csv); ?> Quote Link to comment https://forums.phpfreaks.com/topic/102940-solved-export-db-to-csv-script-help/#findComment-527339 Share on other sites More sharing options...
tqla Posted April 25, 2008 Author Share Posted April 25, 2008 Thanks dptr1988, Actually, I got this script from this board. But it has that bug in it.I see the advantage of fputcsv but I was hoping someone could spot the issue in the current script. Quote Link to comment https://forums.phpfreaks.com/topic/102940-solved-export-db-to-csv-script-help/#findComment-527349 Share on other sites More sharing options...
phpSensei Posted April 25, 2008 Share Posted April 25, 2008 Question isnt very clear to me, can you repeat it? Quote Link to comment https://forums.phpfreaks.com/topic/102940-solved-export-db-to-csv-script-help/#findComment-527361 Share on other sites More sharing options...
dptr1988 Posted April 25, 2008 Share Posted April 25, 2008 I'm on an expert on CSV format, but I think all you need to do is quote your fields, and then the program that reads the data will ignore the newlines that are inbetween quotes. So I don't think that there is a bug in the code, you just need to quote your data. Quote Link to comment https://forums.phpfreaks.com/topic/102940-solved-export-db-to-csv-script-help/#findComment-527372 Share on other sites More sharing options...
tqla Posted April 25, 2008 Author Share Posted April 25, 2008 Oh okay. I got ya. Thanks everybody. Quote Link to comment https://forums.phpfreaks.com/topic/102940-solved-export-db-to-csv-script-help/#findComment-527384 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.