Here's my code:
mysql_set_charset("utf8");
mysql_query("SET NAMES utf8");
header('Content-Type: text/html; charset=utf-8');
mysql_connect("localhost","root","");
mysql_select_db("ladli");
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
$output = "";
$table = "";
$sql = mysql_query("select * from table'");
mysql_query("SET NAMES utf8");
$columns_total = mysql_num_fields($sql);
for ($i = 0; $i < $columns_total; $i++) {
$heading = mysql_field_name($sql, $i);
$output .= '"'.$heading.'",';
}
$output .="\n";
// Get Records from the table
while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$row["$i"].'",';
}
$output .="\n";
}
$filename = "chinese_test.csv";
header('Content-Description: File Transfer');
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
header('Content-Transfer-Encoding: binary');
header('Pragma: public');
print "\xEF\xBB\xBF"; // UTF-8 BOM
print $output;
exit;
In my DB: 測試一下
I the csv: 我是
When I am trying to download a csv using PHP I get messed up csv data with non readable format "我是" etc..
I need to export Chinese words in csv/xsl format from a MySQL database.
Thanks in advance.