php – convert mysql data to csv files
During development sometimes we required php function to convert mysql data to csv files.
Below is the PHP example that takes a mysql query and turns it into a csv file that download file automatically.
Example Code:
- <?php
- header(“Content-type: text/csv”);
- header(“Content-Disposition: attachment; filename=export.csv”);
- header(“Pragma: no-cache”);
- header(“Expires: 0″);
- $mysqlquery=mysql_query(‘select * from employee’);
- while($data_array=mysql_fetch_array($mysqlquery)){
- foreach($data_array as $key => $val){
- if(is_numeric($key)){
- echo ‘”‘.str_replace(‘”‘, ‘”"‘, $val).‘”,’;
- }
- }
- echo “\n”;
- }
- ?>
Hope it will work for you!
Follow @phpzag

เว็บไซต์สวยดีนะคะ แวะมาเยี่ยมชมค่ะ
really good post, i definitely really like this excellent site, maintain on it