Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-04-2009, 11:27 AM   PM User | #1
UD2006
Regular Coder

 
Join Date: Jul 2007
Location: Velsen Noord, Netherlands
Posts: 206
Thanks: 6
Thanked 0 Times in 0 Posts
UD2006 is an unknown quantity at this point
Retrieve data from a mysql database to excel

I have this code that retrieves data from a mysql database and exports it into excel.

Now is my question how to get the column names in excel (above the records).
PHP Code:
<?php 

$DB_Server 
"localhost";        //your MySQL Server  
$DB_Username "username";                 //your MySQL User Name  
$DB_Password "password";                //your MySQL Password  
$DB_DBName "database"

$Connect = @mysql_connect($DB_Server$DB_Username$DB_Password
    or die(
"Couldn't connect to MySQL:<br>" mysql_error() . "<br>" mysql_errno()); 
     
$Db = @mysql_select_db($DB_DBName$Connect
    or die(
"Couldn't select database:<br>" mysql_error(). "<br>" mysql_errno()); 


$query  "SELECT * FROM nbs_vmb_contacts where nbs_vmb_group_id = '2' "
$result mysql_query($query) or die('Error, query failed'); 

$tsv  = array(); 
$html = array(); 
while(
$row mysql_fetch_array($resultMYSQL_NUM)) 

   
$tsv[]  = implode("\t"$row); 
   
$html[] = "<tr><td>" .implode("</td><td>"$row) .              "</td></tr>"


$tsv implode("\r\n"$tsv); 
$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; 
?>
UD2006 is offline   Reply With Quote
Old 09-04-2009, 03:14 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
Just stick em in there prior to writing out the data from the database.
__________________
Fumigator is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:13 AM.


Advertisement
Log in to turn off these ads.