存储非英语字符,得到'?????' - MySQL字符集问题 [英] Stored non-English characters, got '?????' - MySQL Character Set issue

查看:115
本文介绍了存储非英语字符,得到'?????' - MySQL字符集问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的网站位于Farsi,所有文字都显示为(问号)。
我将数据库表格的排序规则更改为UTF8_general_ci,但它仍然显示



我运行以下脚本来更改所有表,但这没有工作以及。



我想知道我做错了什么

  ?php 
//你的连接
mysql_connect(mysql.ord1-1.websitesettings.com,user_name,pass);
mysql_select_db(895923_masihiat);

//转换代码
$ res = mysql_query(SHOW TABLES);
while($ row = mysql_fetch_array($ res))
{
foreach($ row as $ key => $ table)
{
mysql_query TABLE。$ table。CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci);
echo $ key。 =& gt;。 $ table。 CONVERTED< br />;
}
}
?>


解决方案

坏消息。但首先,仔细检查:

  SELECT col,HEX(col)... 

查看表中的内容。如果十六进制显示 3F ,则数据消失。正确存储, dal 字符应为hex D8AF ; hah 是十六进制 D8AD



发生了什么: / p>


  • 您有utf8编码的资料(好)

  • SET NAMES latin1 (默认,但是错误)

  • 该列已声明为 CHARACTER SET latin1 但错误)



由于 INSERTed 数据,所以转换为latin1



修复(对于未来的`INSERT'):




  • 使用mysqli_ *接口而不是弃用的mysql_ *接口重新编码您的应用程序。

  • utf8编码的数据

  • mysqli_set_charset('utf8')

  • 检查列和/或表默认值 CHARACTER SET utf8

  • 如果您在网页上显示,则< meta ... utf8> / li>


上面的讨论是关于字符编码的 CHARACTER SET 现在有关 COLLATION 的提示,用于比较和排序。



如果您希望对其进行处理equal:'بسم'='بسم',然后对 COLLATION 使用utf8_unicode_ci(而不是utf8_general_ci)。


My site that I am working on is in Farsi and all the text are being displayed as ????? (question marks). I changed the collation of my DB tables to UTF8_general_ci but it still shows ???

I ran the following script to change all the tables but this did not work as well.

I want to know what am I doing wrong

<?php
// your connection
mysql_connect("mysql.ord1-1.websitesettings.com","user_name","pass");
mysql_select_db("895923_masihiat");

// convert code
$res = mysql_query("SHOW TABLES");
while ($row = mysql_fetch_array($res))
{
    foreach ($row as $key => $table)
    {
        mysql_query("ALTER TABLE " . $table . " CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci");
        echo $key . " =&gt; " . $table . " CONVERTED<br />";
    }
}
?>

解决方案

Bad news. But first, double check:

SELECT col, HEX(col)...

to see what is in the table. If the hex shows 3F, then the data is gone. Correctly stored, the dal character should be hex D8AF; hah is hex D8AD.

What happened:

  • you had utf8-encoded data (good)
  • SET NAMES latin1 was in effect (default, but wrong)
  • the column was declared CHARACTER SET latin1 (default, but wrong)

As you INSERTed the data, it was converted to latin1, which does not have values for Farsi characters, so question marks replaced them.

The cure (for future `INSERTs):

  • Recode your application using mysqli_* interface instead of the deprecated mysql_* interface.
  • utf8-encoded data (good)
  • mysqli_set_charset('utf8')
  • check that the column(s) and/or table default are CHARACTER SET utf8
  • If you are displaying on a web page, <meta...utf8> should be near the top.

The discussion above is about CHARACTER SET, the encoding of characters. Now for a tip on COLLATION, which is used for comparing and sorting.

If you want these to be treated equal: 'بِسْمِ' = 'بسم', then use utf8_unicode_ci (instead of utf8_general_ci) for the COLLATION.

这篇关于存储非英语字符,得到'?????' - MySQL字符集问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆