Simple MySQL to CSV

/ Published in: PHP
Save to your folder(s)

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.

Copy this code and paste it in your HTML
  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


RSS Icon Subscribe to comments

You need to login to post a comment.