exporting from DB to CSV - encoding issues with arabic characters

datrader

New member
Apr 10, 2011
22
0
0
I’m trying to export from DB content that includes Arabic letters.
When I export to CSV/XLS it appears as ???????? or الØGAYمراوي
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?
 


Try taking out the headers, print it as a normal webpage, then wget that page and look at it in a normal text editor, is it still screwed up? Then it's how you interact with the database probably. If not, it's probably your spreadsheet app.
 
What's the character set of your database? latin1 or utf-8? If latin1 (default upon mySQL install), you're fucked, and those arabic characters are gone, as they wouldn't be stored properly. You'll need to recreate the tables under utf-8, and re-import the arabic rows / characters.

If it's currently latin1, and that database is the only copy of the data you have, then there's not much you can do. You'll just have to accept it as a data loss.