Monday 21, May 2012
Welcome Guest, Register | Login  
      Home    |    Tutorials    |    Free Ebooks    |    Free Scripts    |    Articles    |    Blog     |    About Us    |    Contact Us

  Exporting data from MySql table to CSV using PHP

Here is a code example for exporting data from MySql table to a CSV File. CSV file format is a file type that stores tabular data usually processed in applications like Microsoft Excel.
Cut & Paste Script
<?php
$host = 'localhost';
$user = 'mysqlUser';
$pass = 'myUserPass';
$db = 'myDatabase';
$table = 'products_info';
$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;
?>
 
   
     RELATED SCRIPTS
 
Reversing a string using php
This code example will reverse the given string using php....more »
Category : PHP Examples Hits : 715
   
Difference Between two dates using php
This function will return the number of days elapsed between two dates....more »
Category : PHP Examples Hits : 559
   
setting and retrieving cookies using php
Here is an example to write and retrieve cookies using php. The setcookie() function is used to set a cookie and $_COOKIE variable to retrieve cookie value....more »
Category : PHP Examples Hits : 473
   
Comparing two arrays using php
This code example will take two arrays and compares 2nd array with 1st. It will show the list of elements which are not found in the first array....more »
Category : PHP Examples Hits : 649
   
Uploading file using php
Here is a simple script to upload file using php. we are using move_uploaded_file function to upload file. The move_uploaded_file function needs two parameters
1. The path of the temporary file.
2. The path where it is to be moved to.
...more »
Category : PHP Examples Hits : 429
   
   
SCRIPT CATEGORIES
 
  C Programs
C Programs scripts
  C++ Programs
C++ Programs scripts
  Java Programs
Java Programs scripts
  JavaScript Examples
JavaScript Examples scripts
  PHP Examples
PHP Examples scripts
  VBScript Examples
VBScript Examples scripts
 
 
 
POPULAR E-BOOKS
 
Download Affiliate Marketing Mistakes  Ebook Affiliate Marketing Mistakes
   
Download Create Your First Website By 345 This Afternoon  Ebook Create Your First Website By 345 This Afternoon
   
Download Make Money with Adsense 2008 Ebook Make Money with Adsense 2008
   
Download Step By Step Guide To Creating Your Own RSS Feed  Ebook Step By Step Guide To Creating Your Own RSS Feed
   
Download The Downline Code  Guru Secrets For Explosive MLM Growth  Ebook The Downline Code Guru Secrets For Explosive MLM Growth
   
 
Studiesinn.com © 2012 All Rights Reserved.
Website Designed & Developed by TechXprtz