MySQL 数据库中字符的 UTF-8 问题(例如 é 为 é)
我知道有数百个关于 UTF-8 问题的问题,但我尝试了所有能找到的方法,但没有一个有帮助。
事实: 我正在尝试从 MySQL 数据库中读取包含 é 的字符串并将其显示在 PHP 页面上。实际上,它确实显示为 é(但字体无法识别它,因此使用另一种默认字体)。当我想使用 PHP 字符串替换函数将此字符串转换为文件名时,出现了麻烦。 PHP 根本不将其识别为 é 字符。
以下是我正在做的事情的快速概述:
1) 字符串存储在 MySQL 数据库中。 MySQL 服务器设置为: MySQL连接排序规则utf8_unicode_ci
MySQL 字符集:UTF-8 Unicode (utf8)
数据库本身设置为排序规则utf8_unicode_ci(MyISAM存储引擎,由于共享服务器不可更改)
实际表设置为排序规则 utf8_unicode_ci (InnoDB 存储引擎)
é 在 phpMyAdmin 中正确显示。数据通过 Java 程序插入数据库,但我也尝试过手动输入数据(在 phpMyAdmin 中输入)。
2)PHP default_charset 未设置(无值),我在共享服务器上并且手动覆盖 php.ini 似乎不起作用。使用 ini_set("default_charset", 'utf-8');
有效,但对我遇到的问题没有影响。
3)在运行实际的选择查询之前,我查询SET NAMES 'utf8'
。查询本身无关紧要,但为了测试,我选择了一个简单的 SELECT title FROM items WHERE item_id = 1
4) PHP 文件本身采用 UTF-8 编码。我已经使用 为 html 设置了正确的字符集
5) 测试问题 我在返回的字符串(Astérix)上使用了 htmlentities,检查它转换为 Astérix
的源代码,这当然是不正确的。因此,该字符串在浏览器中显示为 Astérix
。
这可能是什么原因呢?对我来说,好像我将所有可以设置为 UTF-8 的东西都设置了。
I know there are hundreds of questions about UTF-8 woes but I tried all the approaches I could find, none of them helped.
The facts:
I'm trying to read a string that contains a é from my MySQL database and display it on a PHP page. Actually, it does display as é (but the font does not recognize it as such and thus another default font is used). The troubles arose when I wanted to convert this string to a filename using PHP functions for string replacement. PHP does not recognize this as the é character at all.
Here's a quick rundown of what I'm doing:
1) The String is stored in a MySQL database. The MySQL server settings are:
MySQL connection collation utf8_unicode_ci
MySQL charset: UTF-8 Unicode (utf8)
The database itself is set to collation utf8_unicode_ci (MyISAM storage engine, not changeable due to shared server)
The actual table is set to collcation utf8_unicode_ci (InnoDB storage engine)
The é shows up correctly in phpMyAdmin. The data is inserted into the DB via a Java program but I have also tried this with manually entered data (entered in phpMyAdmin).
2) The PHP default_charset is not set (NO VALUE), I'm on a shared server and placing a manual override php.ini did not seem to work. Using ini_set("default_charset", 'utf-8');
works but has no effect on the problem I have.
3) Before I run the actual select query I query SET NAMES 'utf8'
. The query itself is irrelevant but for testing I chose a simple SELECT title FROM items WHERE item_id = 1
4) The PHP file itself is encoded UTF-8. I have set the correct charset for the html with <meta http-equiv="content-type" content="text/html; charset=utf-8" />
5) To test the problem I used htmlentities on the returned string (Astérix), checking the source code it is converted to Astérix
which is not correct of course. Accordingly, the string shows up as Astérix
in the browser.
What possible reason could there be for this? To me it seems like I set everything that can be set to UTF-8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://php.net/manual/en/ref.mbstring.php - 查看多字节字符串函数。
http://php.net/manual/en/ref.mbstring.php - look at multibyte string functions.