Export the database table as CSV format using PHP

This code need some files those files are,
as in the db.php file, make this.

database name –> wallstreet
table name–> export_table
column names –> id,name,place
and as you, here i just declare what i have given in.

$conn = mysql_connect('localhost', 'root', '') or die(mysql_error());
$db=mysql_select_db('wallstreet', $conn) or die(mysql_error());



//header to give the order to the browser
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=exported-data.csv');

//select table to export the data
$select_table=mysql_query('select * from export_table');
$rows = mysql_fetch_assoc($select_table);

if ($rows)
$rows = mysql_fetch_assoc($select_table);

// get total number of fields present in the database
function getcsv($no_of_field_names)
$separate = '';

// do the action for all field names as field name
foreach ($no_of_field_names as $field_name)
if (preg_match('/\\r|\\n|,|"/', $field_name))
$field_name = '' . str_replace('', $field_name) . '';
echo $separate . $field_name;

//sepearte with the comma
$separate = ',';

//make new row and line
echo "\r\n";

this file is just for do the download, just give the link anchor tag,
usually you know to make just click export the data,
for that just give the link on anchor link, that will export the data table.

<a href="export.php"> export the database table </a>

3 thoughts on “Export the database table as CSV format using PHP

  1. Hello i have a problem like that. i have j carousel on my website and when i chose the mobile view jcarousel height width not set message appear.and i use the code twise on same page one for mobile and one for desktop.so on mobile view how can i not load the script which is use for desktop?


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s