...

View Full Version : exporting from DB to CSV - encoding issues with arabic characters



zeemperor
10-09-2012, 09:26 AM
Im 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');





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

Fou-Lu
10-09-2012, 05:40 PM
Open that up in an editor that supports utf8. Does it show the characters as expected?
You'll probably need a content-encoding header as well. Not sure why your content type is text/html, that should be either text/csv or application/csv (or even text/plain), but overall that won't make a tremendous difference to an attachment. You may need to transfer in binary for this as well.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum