2013-03-22 91 views
0

我正在使用雙語(英文&繁體中文)網站,內容存儲在數據庫中。我通常將表格導出爲CSV格式,然後將數據批量輸入,然後將其重新導入到表格中。中文字符顯示在數據庫和網站中。但是,每當我使用繁體中文字符導出表格時,它們都會變成問號。phpmyadmin將表格導出爲CSV,繁體中文字符變成問號

我試着改變整個表的排序規則以及各個列到各種設置(大,二進制,utf8等),但似乎沒有任何工作。我也嘗試在導出界面中使用字符集,但它也不能解決問題。

這是phpmyadmin的問題還是有一些設置可以解決這個問題?你的幫助將不勝感激。

+0

任何人?........ – 2013-03-25 08:50:46

+0

同樣的問題在這裏..仍然未解決.. – rusly 2013-04-25 14:42:07

回答

0

我有點解決了導出表格爲包含中文的csv的問題。但不是真的與phpmyadmin。您可以使用此導出腳本導出需要包含中國表,它會顯示正確

<?php 
/* 
* PHP code to export MySQL data to CSV 
* http://salman-w.blogspot.com/2009/07/export-mysql-data-to-csv-using-php.html 
* 
* Sends the result of a MySQL query as a CSV file for download 
*/ 
/* 
* establish database connection 
*/ 
$conn = mysql_connect('MYSQL_HOST', 'MYSQL_USERNAME', 'MYSQL_PASSWORD') or die(mysql_error()); 
mysql_select_db('MYSQL_DATABASE', $conn) or die(mysql_error($conn)); 
mysql_query("SET character_set_results=utf8", $conn); 
/* 
* execute sql query 
*/ 
$query = sprintf('SELECT * FROM MYSQL_TABLE'); 
$result = mysql_query($query, $conn) or die(mysql_error($conn)); 
/* 
* send response headers to the browser 
* following headers instruct the browser to treat the data as a csv file called export.csv 
*/ 
header('Content-Type: text/csv'); 
header('Content-Disposition: attachment;filename=export.csv'); 
echo "\xEF\xBB\xBF"; 
/* 
* output header row (if atleast one row exists) 
*/ 
$row = mysql_fetch_assoc($result); 
if ($row) { 
echocsv(array_keys($row)); 
} 
/* 
* output data rows (if atleast one row exists) 
*/ 
while ($row) { 
echocsv($row); 
$row = mysql_fetch_assoc($result); 
} 
/* 
* echo the input array as csv data maintaining consistency with most CSV implementations 
* - uses double-quotes as enclosure when necessary 
* - uses double double-quotes to escape double-quotes 
* - uses CRLF as a line separator 
*/ 
function echocsv($fields) 
{ 
$separator = ''; 
foreach ($fields as $field) { 
    if (preg_match('/\\r|\\n|,|"/', $field)) { 
     $field = '"' . str_replace('"', '""', $field) . '"'; 
    } 
    echo $separator . $field; 
    $separator = ','; 
} 
echo "\r\n"; 
} 
?> 

此代碼被複制,並從以下來源修改:

  1. http://salman-w.blogspot.hk/2009/07/export-mysql-data-to-csv-using-php.html
  2. When exported to CSV russian characters won't display

原來問題不在csv文件中,而是在excel中。不管csv文件的編碼是什麼,excel總是將它打開爲ASCII,這就把中文搞砸了。上面的代碼將

  1. 出口CSV爲UTF-8編碼,以便中國可以用這個mysql_query("SET character_set_results=utf8", $conn);
  2. 力顯示擅長與此echo "\xEF\xBB\xBF";

雖然這打開CSV爲UTF-8有點解決了這個問題,如果不需要自定義導出腳本,通過phpmyadmin就可以知道如何做到這一點。

相關問題