1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

exporting from DB to CSV - encoding issues with arabic characters

Discussion in 'PHP & Perl' started by bizbez, Oct 9, 2012.

  1. bizbez

    bizbez Registered Member

    Joined:
    Jan 26, 2011
    Messages:
    86
    Likes Received:
    3
    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:
    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');
    
    


    Anyone has idea how do I fix this? How do I see Arabic letters correctly?
     
  2. CodingAndStuff

    CodingAndStuff Regular Member

    Joined:
    May 6, 2012
    Messages:
    236
    Likes Received:
    84
    Occupation:
    Swagstronaut
    Location:
    You can't have my bots. Sorry :'(
  3. builder

    builder Junior Member

    Joined:
    Jul 31, 2009
    Messages:
    100
    Likes Received:
    17
    Try to find encode utf8 function.