Like Us on Facebook

How to export MySQL Data to CSV file using PHP

Our previous post was How to Import CSV File into MySQL Database using PHP and this post is about How to export MySQL Data to CSV file using PHP. This is easy but important tutorial. In many purpose your client need to download the data from MySQL database. But all the time they do not prefer web page for view. Instead of that they prefer CSV format, developers need to programming.

So using PHP we have done this programming and export MySQL data to CSV file.

Read More How to Import CSV File into MySQL Database using PHP

Export MySQL to CSV file using PHP UandBlog


Lets say, this is home page of your project. You can see the table of your data. And also you can see the download link at the bottom.

<div style="width:300px; margin:0 auto; margin-top:200px;">

<table width="100%" border="0" cellspacing="0" cellpadding="5" style="text-align:center">
  <tr bgcolor="
#000000" style="color:#FFFFFF;">

  $exp_data = mysql_query("select * from export_data");
  while($fet_exp_data = mysql_fetch_assoc($exp_data))

    <td><?php echo $fet_exp_data['name']; ?></td>
    <td><?php echo $fet_exp_data['phone']; ?></td>
    <td><?php echo $fet_exp_data['email']; ?></td>

  <?php } ?>


 <br  />
 <a href="dloadIn.php" style=" text-decoration:none;">
   <span style="background:#FF0066; color:#FFFFFF; padding:5px; cursor:pointer;">Download</span>



Using this PHP page actually main CSV file is being generated. You can write your file name instead your_filename.csv inside header function. Inside this variable $outstr you can store your column title. And all the data of your MySQL table, store in an array variable. Which using join() function put the data into separated places using comma ( , )  inside CSV file.

The join() function returns a string from the elements of an array.


$table = 'export_data';
$outstr = NULL;

header("Content-Type: application/csv");
header("Content-Disposition: attachment;Filename=your_filename.csv");

$conn = mysql_connect("localhost", "root", "");

// Query database to get column names  
$result = mysql_query("show columns from $table",$conn);
// Write column names
 $outstr = 'Name,Phone,Email'."\n";

  $exp_data = mysql_query("select * from export_data");
  while($fetexp_data = mysql_fetch_array($exp_data))
       $arr = array($fetexp_data['name'],$fetexp_data['phone'],$fetexp_data['email']);
       $outstr.= join(',', $arr)."\n";
    echo $outstr;


Database connectivity.  
    error_reporting(E_ALL && ~E_NOTICE);

    $conn = mysql_connect("localhost", "root", "");
    if(!$conn) die("Failed to connect to database!");
    $status = mysql_select_db('import', $conn);
    if(!$status) die("Failed to select database!");

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 0 )

    Leave a comment..

    Click to login.