Posted By

Skippy1957 on 04/22/14


Tagged

exportcsvmysql


Versions (?)

export 2 csv


 / Published in: PHP
 

export csv from mysql php-page

  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);

Report this snippet  

You need to login to post a comment.