Posted By

FatFolderDesigner on 09/03/11


Tagged

mysql php simple csv


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

tux-world


Simple MySQL to CSV


 / Published in: PHP
 

URL: http://fatfolderdesign.com/376/php/simple-mysql-to-csv

Just a short little script that takes a mysql query and turns it into a csv file that downloads automatically. If your interested in the details you can hit up the link.

  1. require '../includes/init.php'; // This connects to the database
  2. header("Content-type: text/csv");
  3. header("Content-Disposition: attachment; filename=client_export.csv");
  4. header("Pragma: no-cache");
  5. header("Expires: 0");
  6. $q=mysql_query('select * from users');
  7. while($r=mysql_fetch_array($q)){
  8. foreach($r as $k => $v){
  9. if(is_numeric($k)){
  10. echo '"'.str_replace('"', '""', $v).'",';
  11. }
  12. }
  13. echo "\n";
  14. }

Report this snippet  

You need to login to post a comment.