converting MYSQl result into csv file format


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

when im running the program the browser forced to download the file which i convert using php code(it askng do u want to download). what i want is simply run the program and convert the file as csv and csv file has to open automatically(no need of downloading dialouge box). plz let me know the solution well in advance.


Copy this code and paste it in your HTML
  1. $query = "SELECT name, email,address FROM table name";
  2. $result = $db->sql_query($query);
  3.  
  4. $file_name = "example.csv";
  5.  
  6.  
  7. header("Content-type: text/x-csv");
  8. header("Content-Disposition: attachment; filename=$file_name");
  9. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  10. header("Pragma: no-cache");
  11. header("Expires: 0");
  12. while ($data = mysql_fetch_array($result))
  13. {
  14. echo($data['name']);
  15. echo",";
  16. echo($data['email']);
  17. echo",";
  18. echo($data['address']);
  19. echo",";
  20. echo("\t\n");
  21. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.