Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Location
    Velsen Noord, Netherlands
    Posts
    218
    Thanks
    6
    Thanked 0 Times in 0 Posts

    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; 
    ?>

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Just stick em in there prior to writing out the data from the database.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •