how to generate Csv File in php

<?php
error_reporting(0);
mysql_connect(‘localhost’, ‘root’, ”);
mysql_select_db(‘examples’);
$qry = mysql_query(“SELECT * FROM contact”);
$data = “”;

while($row = mysql_fetch_array($qry)) {
$data .= $row[‘id’].”,”.$row[‘name’].”,”.$row[’email’].”,”.$row[‘message’].”,”.$row[‘subject’].”\n”;
}

$file = ‘file.csv’;
file_put_contents($file, $data);
echo ‘CSV File is Generated.’;
?>

==========================================

<?php
$list = array
(
“Peter,Griffin,Oslo,Norway”,
“Glenn,Quagmire,Oslo,Norway”,
);

$file = fopen(“contact.csv”,”w”);

foreach ($list as $line)
{
fputcsv($file,explode(‘,’,$line));
}
echo ‘Your CSV File is Generated.’;
fclose($file); ?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s