Converting Mysql data to Excel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robin1983
    New Member
    • Oct 2007
    • 99

    Converting Mysql data to Excel

    Dear All,

    I am trying to download the data from mysql to excel (CSV). I get a quote but when i download the data, its fetching the only data but I want to put the column name also.

    Code:
     <?php
    include_once("db_connect.inc.php");
    
    $query  = "SELECT * FROM logintable";
    $result = mysql_query($query) or die('Error, query failed');
    
    $tsv  = array();
    $html = array();
    while($row = mysql_fetch_array($result, MYSQL_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 = 'logintable.xls';
    header("Content-type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=$fileName");
    
    echo $tsv;
    //echo $html;
    
    ?>
    Please help me in putting the column name. in the excel file.

    Thanks in advance

    Regards,
    Robin.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Can you clarify the question please: you need to get the MySQL column titles also?

    Comment

    • robin1983
      New Member
      • Oct 2007
      • 99

      #3
      Hi..
      sure, I want to to download the data from mysql to excel. I am able to download with the above code but the problem is that when I download the data into excel the column name of the table is not download. It download the only the data. Eg. table1 contain c1, c2 column
      C1 C2
      abc india
      xyz india

      with the above code, i can download abc, india but not the column name C1 and C2 not in the excel.

      Kindly help in downloading column name also.


      With Regards,

      Robin

      Comment

      Working...