SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    HAHA!
    Join Date
    Mar 2006
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    export .csv and set charset

    I found this code for exporting into a .csv on phpsimple.

    Code PHP:
    <?
    // Connect database
    $database="tutorial";
    $table="name_list";
    mysql_connect("localhost","","");
    mysql_select_db("tutorial");
     
    $result=mysql_query("select * from $table");
     
    $out = '';
     
    // Get all fields names in table "name_list" in database "tutorial".
    $fields = mysql_list_fields(tutorial,$table);
     
    // Count the table fields and put the value into $columns.
    $columns = mysql_num_fields($fields);
     
     
    // Put the name of all fields to $out.
    for ($i = 0; $i < $columns; $i++) {
    $l=mysql_field_name($fields, $i);
    $out .= '"'.$l.'",';
    }
    $out .="n";
     
    // Add all values in the table to $out.
    while ($l = mysql_fetch_array($result)) {
    for ($i = 0; $i < $columns; $i++) {
    $out .='"'.$l["$i"].'",';
    }
    $out .="n";
    }
     
    // Open file export.csv.
    $f = fopen ('export.csv','w');
     
    // Put all values from $out to export.csv.
    fputs($f, $out);
    fclose($f);
     
    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename="export.csv"');
    readfile('export.csv');
    ?>

    The only thing left to make me happy is if I could set the charset to UTF8 so that it's recognised by the .csv and recognised by all programs I might import this .csv into such as excel.
    Cheap web hosting directory listing the cheapest web hosting

    Submit articles to an article directory

  2. #2
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    have you tried adding a utf8 BOM to the front of the file?
    Check out our new Industry News forum!
    Keep up-to-date with the latest SP news in the Community Crier

    I edit the SitePoint Podcast

  3. #3
    HAHA!
    Join Date
    Mar 2006
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no, what is a utf8 BOM and how do I do that?
    Cheap web hosting directory listing the cheapest web hosting

    Submit articles to an article directory

  4. #4
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    change this:
    PHP Code:
    $out ''
    to this:
    PHP Code:
    $out = (string)0xefbbbf
    (untested)
    Check out our new Industry News forum!
    Keep up-to-date with the latest SP news in the Community Crier

    I edit the SitePoint Podcast


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
  •