SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot JordanTLClive's Avatar
    Join Date
    Feb 2001
    Location
    Las Vegas, NV
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to export data from a MySQL database in CSV format. The trouble is... I'm not quite sure how to do this Can anyone help me figure out how to do this? Thanks!
    -Jordan

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should do it

    PHP Code:
    <?
    $db 
    mysql_connect("localhost""username""password");
    mysql_select_db("dbname");

    $fieldq mysql_list_fields("dbname""tablename");
    $columns mysql_num_fields($fieldq);

    for (
    $i 0$i $columns$i++) {
        
    $fields[] = mysql_field_name($fieldq$i);
        }
        print 
    implode(","$fields)."\n";  

    $r mysql_query("select * from tablename");

    header("Content-Type: application/octet-stream");
    header("Content-disposition: attachment;filename=test.csv");


    while(
    $row mysql_fetch_array($r)) {
        foreach(
    $fields as $field) {
            
    $tmp[] = $row[$field];
            }
        print 
    implode(","$tmp)."\n";  
        unset(
    $tmp);
        }
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •