Mar 14, 2009
admin

MySQL table to CSV – PHP Script

Hello Friends !!

If you want to export data from MySQL table to CSV file ,then don’t worry. I will provide you one script. When you run this script, data in table are stored in CSV file.

Here i give you code of the PHP script. You have to just copy that code and paste in your PHP file and run that file.

<?php
$host = ‘hostaddress’;
$user = ‘username’;
$pass = ‘password’;
$db = ‘DB name’;
$table = ‘table name’;
$file = ‘export’;

$link = mysql_connect($host, $user, $pass) or die(“Can not connect.” . mysql_error());
mysql_select_db($db) or die(“Can not connect.”);

$result = mysql_query(“SHOW COLUMNS FROM “.$table.”");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].”; “;
$i++;
}
}
$csv_output .= “\n”;

$values = mysql_query(“SELECT * FROM “.$table.”");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].”; “;
}
$csv_output .= “\n”;
}

$filename = $file.”_”.date(“Y-m-d_H-i”,time());
header(“Content-type: application/vnd.ms-excel”);
header(“Content-disposition: csv” . date(“Y-m-d”) . “.csv”);
header( “Content-disposition: filename=”.$filename.”.csv”);
print $csv_output;
exit;
?>

If you hace any query then comment me.

Thanks in advance….

  • http://www.docfree.co.cc/mysql-table-to-csv-php-script-php-genious-805.html MySQL table to CSV – PHP Script | php genious « Online Free Document Archive

    [...] Originally posted here: MySQL plateau to CSV – PHP Script | php genious [...]

  • huzefa

    Hiiii

    I am getting the output on the screen but not in the downloadable csv file, I am using localhost.

    can u plz help me out with this problem

  • huzefa

    Hiiii

    I am getting the output on the screen but not in the downloadable csv file, I am using localhost.

    can u plz help me out with this problem

  • viktor

    Thanks for the code a lot! Several improvements to suggest:
    1. Won’t it be better to use chr(13) instead of ‘n’? 2. Also to save the file instead of printing it to browser you should use: header(“Content-Disposition: attachment; filename=”.$filename.”.csv”);

  • viktor

    Thanks for the code a lot! Several improvements to suggest:
    1. Won’t it be better to use chr(13) instead of ‘\n’? 2. Also to save the file instead of printing it to browser you should use: header(“Content-Disposition: attachment; filename=”.$filename.”.csv”);

  • http://www.facebook.com/hephaestoskin Benjamin Morrison

    Could this be modified to email the generated file?

PHP Genious Services

I am a PHP freelancer india, PHP Developer india, PHP programmer india, Wordpress Freelancer india, Wordpress customization services, Wordpress Plugin Developer, Wordpress theme customization, Wordpress plugin customization, Magento Freelancer, Magento Developer india, Magento customization services, Magento theme integration, Opencart Developer india, Zencart Freelancer, Opencart customization, Opencart plugin development, HTML and CSS customization, cakePHP Developer, cakePHP Freelancer india, Ecommerce Developer india

Click Here for

Follow me on Facebook

Categories

Get Adobe Flash playerPlugin by wpburn.com wordpress themes