我必须将.txt文件中的数据导入mySQL db。
当我用记事本打开txt时,文件是用DOS / Windows ANSI编写的(用ANSI编码)。 当我用AkelPad打开它时,我得到文件以1253(ANSI-Greek)
编码的信息我使用以下代码导入数据
$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 :(得分:0)
有几件事可能是错的
1)您确定在数据库中使用UTF-8数据库,表,列,并尝试保存数据吗?
2)你运行mysql_query(“SET NAMES utf8”);在运行select / update到数据库之前?
如果你做得对,那我建议你更详细地调试一下。
3)尝试在txt文件中指定一个字符并尝试在iconv函数之前和之后回显ord($ str {xxx})并检查二进制数据是否真的是UTF-8正确编码。