Friday, 21 December 2012

Generate Csv file using PHP

<?php
ob_start();
session_start();
include('config.php');

// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=filename.csv');

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// output the column headings
//fputcsv($output, array('Subscribed User '));
fputcsv($output, array('Field1', 'Field2 ', Field3 '));

$rows = mysql_query("SELECT   Field1, Field 2, Field 3  FROM table ");

// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);

?>

No comments:

Post a Comment