2011-10-02 43 views
2

我正在面對將希伯來語文本插入到mysql中的奇怪問題。

基本上問題是:
我有一個PHP腳本,它從csv文件中拾取希伯來語文本,然後將其發送到mysql數據庫。數據庫和表的所有字段的字符集都設置爲UTF8,並且整理爲utf8_bin。但是當我使用mysql插入它時,隨機垃圾值出現在文本內部,導致它完全無用於輸出。注:我仍然可以看到一半的單詞正確顯示。

這裏是我的作業,它可能會幫助你理解:
1.正如我所提到的表字符集和整理是utf8。
2.我發送的頭文件('Content-Type:text/html; charset = utf-8')
3.如果我回顯文本,它看起來很完美。當我使用utf-8_encode 進行轉換時,它會正確轉換。 (例如,您的轉換爲ש×××××)
4.當我在轉換的變量上使用utf-8_decode並使用echo時,它仍然完美顯示。
5.我使用mysql_connect後的這些數據
使用PHP(垃圾文本)將希伯來語文本插入到MySQL中

mysql_query(「SET character_set_client ='utf8';」);
mysql_query(「SET character_set_result ='utf8';」);
mysql_query(「SET NAMES'utf8'」);
mysql_set_charset('utf8');

,甚至試圖這樣:
的mysql_query( 「SET character_set_results = 'utf-8',character_set_client字符= 'utf-8',是character_set_connection = 'utf-8',character_set_database = 'utf-8',被character_set_server = 'utf-8'」,$ CON)

  1. 在我的php.ini文件中添加了default_charset =「UTF-8」。
  2. 我不知道在csv文件中使用的編碼,但是當我用notepad ++打開它時,編碼是utf-8,沒有BOM。
  3. 下面是實際的垃圾樣本:
    原文:שי函數utf8_encode後יפת
    文本:ש××פת
    在同一個腳本utf8_decode後的文本:שייפת(完美)
    文本發送到MySQL數據庫:ש×? ×?פת(注意兩者之間)
    文本如果我們從mysql回顯:ש ? (輸出關閉)
  4. utf8_encoding之前使用的addslashes和stripslashes。 (即使沒有運氣試過)
  5. 服務器是運行在Windows XAMP 1.7.4
    • 的Apache 2.2.17
    • 的MySQL 5.5.8(社區服務器)
    • PHP 5.3。5(VC6 X86 32位)

編輯1:只是爲了澄清,我並搜索類似問題的網站,並實施中發現的建議(集名UTF8和很多其他的選擇等),但它沒」 t鍛鍊。所以請不要將這個問題標記爲重複。

編輯2: 以下是完整的腳本:

<?php 
header('Content-Type: text/html; charset=utf-8'); 

if (isset($_GET['filename'])==true) 
{ 
$databasehost = "localhost"; 
$databasename = "what_csv"; 


$databaseusername="root"; 
$databasepassword=""; 
$databasename= "csv"; 

$fieldseparator = "\n"; 
$lineseparator = "@contact\n"; 


$csvfile = $_GET['filename']; 
/********************************/ 


if(!file_exists($csvfile)) { 
    echo "File not found. Make sure you specified the correct path.\n"; 
    exit; 
} 

$file = fopen($csvfile,"r"); 

if(!$file) { 
    echo "Error opening data file.\n"; 
    exit; 
} 

$size = filesize($csvfile); 

if(!$size) { 
    echo "File is empty.\n"; 
    exit; 
} 

$csvcontent = fread($file,$size); 

fclose($file); 

$con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error()); 

mysql_query("SET NAMES utf8"); 
mysql_set_charset('utf8',$con); 
/* 
mysql_query("SET character_set_client = 'utf8';"); 
mysql_query("SET character_set_result = 'utf8';"); 

mysql_query("SET NAMES 'utf8'"); 
mysql_set_charset('utf8'); 

mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $con); 
*/ 

@mysql_select_db($databasename) or die(mysql_error()); 



$lines = 0; 
$queries = ""; 
$linearray = array(); 

foreach(explode($lineseparator,$csvcontent) as $line) { 

$Name=""; 
$Landline1=""; 
$Landline2=""; 
$Mobile=""; 
$Address=""; 
$Email=""; 
$IMEI="temp"; 
$got_imei=false; 

//echo $line.'<br>'; 
    $lines++; 

    $line = trim($line," \t"); 

    $line = str_replace("\r","",$line); 

    $linearray = explode($fieldseparator,$line); 
    //check for values to insert 
    foreach($linearray as $field) 
    { 
    if (is_numeric($field)){ $got_imei=true;$IMEI=trim($field);} 
    if (stristr($field, 'Name:')) {$Name=trim(str_replace("Name:", "", $field));} 
    if (stristr($field, 'Landline:')) {$Landline1=trim(str_replace("Landline:", "", $field));} 
    if (stristr($field, 'Landline2:')) {$Landline2=trim(str_replace("Landline2:", "", $field));}  
    if (stristr($field, 'Mobile:')) {$Mobile=trim(str_replace("Mobile:", "", $field));} 
    if (stristr($field, 'Address:')) {$Address=trim(str_replace("Address:", "", $field));} 
    if (stristr($field, 'Email:')) {$Email=trim(str_replace("Email:", "", $field));} 



    } 
    if ($got_imei==true) 
    { 

    $query = "UPDATE $databasetable SET imei=$IMEI where imei='temp'"; 
     mysql_query($query); 

    } 



    else if (($Name=="") && ($Landline1=="") && ($Landline2=="") && ($Mobile=="") && ($Address=="")) {echo "";} 
    else 
    { 
     //$Name = utf8_encode("$Name"); 
     //$Name = addslashes("$Name"); 
     $Name = utf8_encode(mysql_real_escape_string("$Name")); 

     echo"$Name,$Landline1,$Landline2,$Address,$IMEI<br>"; 
     $query = "insert into $databasetable (imei, name, landline1, landline2, mobile, address, email) values('$IMEI','$Name', '$Landline1','$Landline2','$Mobile', '$Address', '$Email');"; 
     mysql_query($query); 
     $Name = utf8_decode(($Name)); 
     echo $Name."<br>"; 

    } 
} 
@mysql_close($con); 



echo "Found a total of $lines records in this csv file.\n"; 

} 
?> 


<form> 
Enter file name <input type="text" name="filename" /><br /> 
<input type="submit" value="Submit" /><br> 
NOTE : File must be present in same directory as this script. Please include full filename, for example filename.csv. 
</form> 

下面是CSV文件的樣本:

@contact 
Name: שי יפת 
Mobile: 0547939898 

@IMEI 
355310042074173 

編輯3:

如果我直接進入通過串cmd我得到這個警告:

Warning Code : 1366 
Incorrect string value: '\xD7\xA9\xD7\x99 \xD7...' for column 'name' at row 1 

這是我在網上發現的可能相關的東西,有什麼幫助嗎? http://bugs.mysql.com/bug.php?id=30131

+1

我使用utf8_general_ci,也許它會爲你工作好。 utf8沒有BOM是好的btw。使用mysql_real_escape_string而不是addslashes – galchen

+0

感謝您的快速響應,但我忘了提到我已經嘗試了兩種,mysql_real_escape_string和更改和更改排序規則到utf_general_ci,沒有運氣。 – Ajit

回答

1

使用文本/長文本,而不是VARCHAR。還可以使用排序規則utf8_general_ci

希望這將幫助你@Ajit

+0

升級後,但設置爲文字解決了這個問題(雖然不是一個合適的解決方案,因爲我確實想要搜索varchar類型等)。 – Ajit

2

我也有這個問題。 Thees行解決這個問題:

mysql_query("SET NAMES utf8"); 
mysql_query("SET CHARACTER SET utf8"); 

沙娜娃

+0

嗨,還是那些?在mysql表中。 – Ajit

+0

是否將保存csv到數據庫的php文件也編碼在沒有BOM的utf8中? –

+0

嗨,它在ANSI編碼只是在UTF中創建該文件的副本,仍然相同的垃圾條目! – Ajit

相關問題