TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-23-2008, 06:08 AM   #1 (permalink)
The Visitor
 
sharma.9.pooja's Avatar
 
Join Date: Jun 2008
Posts: 4
Thanks: 1
sharma.9.pooja is on a distinguished road
Default want to fetch data in excel sheet from my mysql database

hi all,

i want to fetch my data from database in excel sheet.please anybody help me regarding this problem..
this is my code........

<?php
$local = "localhost";
$user = "root";
$pass = "root";
$db = "contact1";


$conn = mysql_connect($local,$user,$pass) or die ("Unable to connect to database.");

mysql_select_db($db, $conn);
$sql = mysql_query("SELECT * FROM submit");
$result = mysql_query($sql) or die('Error, query failed');
$tsv = array();
$html = array();
while($row = mysql_fetch_array($result))
{
$tsv[] = implode("\t", $row);
$html[] = "<tr><td>" .implode("</td><td>", $row) .<br>
"</td></tr>";
}

$tsv = implode("\r\n", $tsv);
echo $html = "<table>" . implode("\r\n", $html) . "</table>";

$fileName = 'mysql-to-excel.xls';
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$fileName");

//echo $tsv;
echo $html;
mysql_close($conn);

?>

sharma.9.pooja is offline  
Reply With Quote
Old 06-23-2008, 02:45 PM   #2 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 836
Thanks: 31
sketchMedia is on a distinguished road
Default

Why not do it to CSV, tested and this works for Open Office so it should work for excel (but microsoft being evil, it probably wont), there are also some bits of incorrect code within your example which i have fixed in mine:
PHP Code:
$conn mysql_connect($local,$user,$pass) or die ("Unable to connect to database.");

mysql_select_db($db$conn);

$result mysql_query("SELECT * FROM users") or die('Error, query failed');
$csv '';
$fileName 'test.csv';
while(
$row mysql_fetch_array($result))
{
    
$csv .= implode($row',')."\n";
}

header("Content-Type: text/comma-separated-values");
header("Content-Disposition: attachment; filename=$filename");
echo 
$csv
Hopefully that will give you what you need to get going.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 06-24-2008 at 08:22 AM. Reason: removed 'var_dump' left it in by accident when testing
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
sharma.9.pooja (06-24-2008)
Old 06-24-2008, 05:04 AM   #3 (permalink)
The Visitor
 
sharma.9.pooja's Avatar
 
Join Date: Jun 2008
Posts: 4
Thanks: 1
sharma.9.pooja is on a distinguished road
Default

hey,
really thankful for ur suggestion ....its wrking fine
but if we use the header type Content-type: application/vnd.ms-excel
we can easily save our data in .xls file and change the file name too

thanks pooja
sharma.9.pooja is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:19 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design