2013-05-10 49 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