SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Export From MySQL to Excel/CSV

    Hi

    How can I do a query from a MySQL database and export to Excel/CSV?
    Is there a class or function that can do that?

    Thanks.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can easily do this with phpmyadmin if its installed.

  3. #3
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks calm.

    How could be done thru phpmyadmin...

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    -goto phpmyadmin
    -on the left, there should be a menu to select which database to work on. pick one.
    -now look to the top of the screen, you should see an export tab. click.
    - now you should see a list of tables, pick 1 or shift click multiples. pick the output format and any other options you want. then theres a "go" button to finish.

  5. #5
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks clam.

    But It can be done as manually. If any possible way to done thru coding. That is the user clicks on the "export" button ,and the mysql datas are export into .CSV file.

  6. #6
    Non-Member Gator99's Avatar
    Join Date
    Sep 2004
    Location
    Florida
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not exactly rocket science. You do a query, then format the results in csv format. Here's an example:
    Code:
    $lines="";//or you can put the column names here, theres a query to get those but it's beyond the scope of this example
    $sql="select * from my great_table";
    $result=mysql_query($sql);
    while($row=mysql_fetch_assoc($result)){
      $tmp=array();
      foreach($row as $k => $v){
        array_push($tmp,$v);
      }
      $lines.='"'.implode("\",\"",$tmp)."\"\n";
    }
    header("Content-Type: application/octet-stream");
    header("Content-Length: ".strlen($lines));
    header("Content-Disposition: attachment;filename=mygreatdata.csv");
    print $lines;


Bookmarks

Posting Permissions

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