CSV (comma-separated values) is the most widely supported format for transferring tabular data between applications. The ability to export data in CSV format is a useful feature for many programs, and is becoming increasingly common in web applications. This page explains how to use PHP to create CSV files, and how to ensure that your visitor’s browser offers to download the file instead of displaying it. |
<?php header("Content-type:application/octet-stream"); header("Content-Disposition:attachment;filename='PHP Download as CSV.csv'"); $out = fopen('php://output', 'w'); # If you want to process static data fputcsv($out, array("Name", "Roll")); fputcsv($out, array("Pritom 1", "Roll 1", "Extra 1")); fputcsv($out, array("Pritom 2", "Roll 2", "Extra 2")); # Fetch data from MySQL mysql_connect('localhost', 'root', ''); mysql_select_db('test'); $rows = mysql_query('SELECT * FROM t1'); // loop over the rows, outputting them while ($row = mysql_fetch_assoc($rows)) fputcsv($out, $row); fclose($out); |
In a real application the database connection parameters should be defined as constants in a separate configuration file. |
0 comments:
Post a Comment