2013-05-10 29 views
0

我必須將數據從.txt文件導入到mySQL數據庫。php插入文本文件到mysql編碼

當我用記事本打開txt時,文件被寫入DOS/Windows ANSI(ANSI編碼)。 當我與AkelPad打開它,我得到的文件在1253(ANSI-希臘)編碼的信息

我導入數據與下面的代碼

$myFile = "Bill.txt"; 
$fh = fopen($myFile, 'r'); 
$theData1 = fread($fh, filesize($myFile)); 
$theData = str_replace("'","#",$theData1); 
$theData = iconv('cp1253','UTF-8',$theData); 
$data = (filesize($myFile))/156; 
fclose($fh); 
$line = 0; 

mysql_query("TRUNCATE Bill"); 
for ($counter = 1; $counter <= $data; $counter++) { 
    $Barcode[counter] = substr($theData, ($line+0), 10); 
    $BuildingCode[counter] = substr($theData, ($line+10), 5); 
    $BuildingAddress[counter] = substr($theData, ($line+14), 40); 
    $FlatName[counter] = substr($theData, ($line+54), 50); 
    $FlatDescription[counter] = substr($theData, ($line+104), 8); 
    $EntrySeason[counter] = substr($theData, ($line+112), 23); 
    $Period[counter] = substr($theData, ($line+135), 11); 
    $Amountint = substr($theData, ($line+146), 7); 
    $Amountdec = substr($theData, ($line+153), 2); 
    $Amount[counter] = $Amountint.'.'.$Amountdec; 

    mysql_query("INSERT INTO Bill (Id, Code, Address, Name, Description, EntrySeason, Period, Revenue) 
    VALUES ('$Barcode[counter]', '$BuildingCode[counter]', '$BuildingAddress[counter]', '$FlatName[counter]', '$FlatDescription[counter]', '$EntrySeason[counter]', '$Period[counter]', '$Amount[counter]')"); 

    $line = $counter * 156; 
} 

當我讀的代碼上面的數據

$query = "SELECT * FROM Bill ORDER BY Id ASC"; 
$result = mysql_query($query) or die(mysql_error()); 
$i=0; 
while ($row=mysql_fetch_array($result)){ 
$i=$i+1; 
echo $i." - ".$row['Id']. " - ". $row['Code']. " - ". $row['Address']. " - ". $row['Name']. " - ". $row['Description']. " - ". $row['EntrySeason']. " - ". $row['Period']. " - ". $row['Revenue'].'<br>'; 

我收到希臘字母。當我檢查數據庫與phpMyAdmin所有希臘條目都是這樣的字符ÃÅÙÑÃ.ÃÅÍÍÇÌÁÔÁ 39Á

如果我嘗試從MySQL插入entrys對於SQLite的信件是這些ÃÅÙÑÃ.ÃÅÍÍÇÌÁÔÁ 39Á

我在哪裏錯了?

回答

0

有幾件事情,可能是錯的

1)你確定你使用的是UTF-8數據庫,表,列在數據庫中,你要保存的數據? 2)你運行mysql_query(「SET NAMES utf8」);你運行mysql_query(「SET NAMES utf8」);你運行mysql_query(「SET NAMES utf8」);在運行數據庫的選擇/更新之前?

如果你正在做對,那麼我建議你更詳細地調試。

3)嘗試指定txt文件中的一個字符,並嘗試在iconv函數前後回顯ord($ str {xxx}),並檢查二進制數據是否真正爲UTF-8正確編碼。

+0

我嘗試以下我從我的TXT的第一個字母的回聲ORD,我收到208. iconv iconv('CP1253','UTF-8',$ theData);我收到206 ...有什麼需要做的? – kosbou 2013-05-11 10:28:00

+0

什麼是正確的字符。應該是206還是208?當你試圖從數據庫中獲得這個數據時(又是在ord中),你會得到什麼? – Ondrej 2013-05-13 15:08:28