2016-04-04 6 views
0

mysqlからいくつかの中国語を含むcsvにデータをエクスポートしようとしていますが、常にガベージコードになります。私はグーグルで、BOMを見出しに追加する提案を見つけました。しかし、それはまだ動作していないようです、ここで私のコードです。提案してください。PHPはcsvにmysql chinese文字をエクスポートしますが、文字はガベージコードになります

<?php 
if(isset($_POST["Export"])) 
{ 
mysql_connect('localhost','test','abc'); 
mysql_select_db('test'); 

header('Content-Encoding: UTF-8'); 
header('Content-Type: text/csv; charset=utf-8'); 
header(sprintf('Content-Disposition: attachment; filename=my-csv-%s.csv', date('dmY-His'))); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 

$data = fopen('php://output', 'w'); 

//This line is important: 
fputs($data, "\xEF\xBB\xBF"); 

fputcsv($data,array('student_id','student_chiName','student_engName',' student_title','student_gender','news_receive')); 


//Retrieve the data from database 
$rows = mysql_query('SELECT * FROM student'); 

//Loop through the data to store them inside CSV 
while($row = mysql_fetch_assoc($rows)){ 
fputcsv($data, $row); 

} 
} 

?> 

ここは私の16進表示です。中国語の文字は1バイトのみで、1バイトは欠落しているようです。 PHPは文字を4バイトで出力しないようです。 The hex dump of output file by PHP

答えて

0

これを試してみてください:

$data = fopen($filepath, 'w'); 
$rows = mysql_query('SELECT * FROM student'); 

fprintf($df, chr(0xEF).chr(0xBB).chr(0xBF)); 
while($row = mysql_fetch_assoc($rows)){ 
    fputcsv($data, $row); 
} 

ラインfprintf($df, chr(0xEF).chr(0xBB).chr(0xBF));は正しいエンコーディング用ファイルヘッダを書き込みます。

関連する問題