我将希伯来文本插入mysql时面临一个奇怪的问题
基本上问题是:
我有一个PHP脚本,从csv文件中获取希伯来文本,然后将其发送到mysql数据库。数据库和表的所有字段的字符集都设置为UTF8,排序规则设置为utf8_bin。但是当我使用mysql插入它时,文本中会出现随机垃圾值,这使得它对输出完全没用。注意:我仍然可以看到一半的单词正确显示
这是我的作业,可以帮助你理解:
正如我所提到的,表格字符集和整理是utf8
2.我发送标题('Content-Type:text / html; charset = utf-8')
如果我回应文本,它看起来很完美。当我使用utf-8_encode转换它时
它得到了正确的转换。 (例如,שיית转换为×××××××))
4.当我在转换变量上使用utf-8_decode并使用echo时,它仍然可以完美显示
5.我在mysql_connect之后使用了这些
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)
编辑1:只是为了澄清我在网站上搜索了类似的问题并且确实实现了找到的建议(SET NAME UTF8和其他选项等),但它没有成功。所以请不要将此问题标记为重复。
编辑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
答案 0 :(得分:2)
我也有这个问题。 Thees线解决了它:
mysql_query( "SET NAMES utf8" );
mysql_query( "SET CHARACTER SET utf8" );
Shana Tova
答案 1 :(得分:1)
使用Text / LongText而不是varchar。也可以使用Collation作为utf8_general_ci
希望这会帮助你@Ajit