Export MySql Data to a csv file using php

<?
$filename = $sitename . ‘my csv.’ . date( ‘Y-m-d-H-i-s’ ) . ‘.csv’;

header( ‘Content-Description: File Transfer’ );
header( ‘Content-Disposition: attachment; filename=’ . $filename );
$fields=”ID”.”,”.”Property_Title”.”,”.”Description”.”,”.”property_type”.”,”.”location”.”n”;
$data=””;
$querypl= “SELECT * FROM mytable”;
$resultsrows = mysql_query($querypl);
while($rowpl = mysql_fetch_array($resultsrows))
{
$ID=$rowpl[‘ID’];
$property_title=$rowpl[‘property_title’];
$property_desc=$rowpl[‘property_desc’];
$property_type=$rowpl[‘property_type’];
$location=$rowpl[‘location’];

$data .= $ID”.”,”.$property_title.”,”.$property_desc.”,”.$property_type.”,”.$location.”n”;
}

echo $fields.$data;
exit;

?>

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