We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

anupaddu on 12/27/07


Tagged


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

vali29
skywalker


converting MYSQl result into csv file format


Published in: PHP 


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.

  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 

You need to login to post a comment.