Forum Moderators: coopster
header( "Content-Type: application/save-as" );
header ('Content-Disposition: attachment; filename="myfile.csv"');
Otherwise a csv file is just a comma delimited file, output each record with commas between the fields and ensure each record is on it's own individual line.
Hope this helps.
1 connect to db
2 select desired records
3 create or open the destination file
4 step through the returned records writing them line by line into your destination file
5 close your destination file
mysql_connect
mysql_select_db
mysql_query
(you may want to check for 1 or more rows returned here)
fopen
mysql_fetch_array
- fwrite
fclose
take each column returned in the row and concatenate it with a comma and end it with a newline
let's say there are 3 columns
$fp = fopen('/path/to/file.csv','w');
$q = 'select * from mytable';
$query = mysql_query($q);
while ($row = mysql_fetch_array($query)) {
$nextline = $row[0] . ',' . $row[1] . ',' . $row[2] . "\r\n";
fwrite($fp,$nextline);
}
fclose($fp);
something like that