1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

exporting from DB to CSV - encoding issues with arabic characters

Discussion in 'PHP' started by datrader, Oct 9, 2012.

  1. #1
    I’m trying to export from DB content that includes Arabic letters.
    When I export to CSV/XLS it appears as ???????? or الحمراوي
    I use this code:
    
    $query = "SET NAMES 'utf8' COLLATE 'utf8_general_ci'";
    mysql_query($query) or die(mysql_error());
    
    
    function CSVExport($query,$filename = 'data') {
        $sql_csv = mysql_query($query) or die("Error: " . mysql_error()); 
        header('Content-Type: text/html; charset=UTF-8');
        //header("Content-type: application/vnd.ms-excel");
        header("Cache-Control: cache, must-revalidate");
        header("Pragma: public"); 
        header("Content-type:text/octect-stream");
        header("Content-Disposition:attachment;filename=$filename.csv");
        
        while($row = mysql_fetch_row($sql_csv)) {
            print '"' . stripslashes(implode('","',$row)) . "\"\n";
        }
        exit;
    }
    
    
     CSVExport("SELECT * from `arabictable`", 'myexportfile');
    
    
    Code (markup):


    Anyone has idea how do I fix this? How do I see Arabic letters correctly?
     
    datrader, Oct 9, 2012 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Are you setting a charset in the content type header for the file? Also, what is the collation of the tables in the database?
     
    jestep, Oct 9, 2012 IP