×

Welcome to TagMyCode

Please login or create account to add a snippet.
1
0
 
1
Language: PHP
Posted by: Skippy1957
Added: Apr 22, 2014 1:53 PM
Views: 23
Tags: no tags
  1. // output headers so that the file is downloaded rather than displayed
  2. header('Content-Type: text/csv; charset=utf-8');
  3. header('Content-Disposition: attachment; filename=data.csv');
  4.  
  5. // create a file pointer connected to the output stream
  6. $output = fopen('php://output', 'w');
  7.  
  8. // output the column headings
  9. fputcsv($output, array('Column 1', 'Column 2', 'Column 3'));
  10.  
  11. // fetch the data
  12. mysql_connect('localhost', 'username', 'password');
  13. mysql_select_db('database');
  14. $rows = mysql_query('SELECT field1,field2,field3 FROM table');
  15.  
  16. // loop over the rows, outputting them
  17. while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);