phpmyadmin 显示奇怪的字符(utf8)

Posted

技术标签:

【中文标题】phpmyadmin 显示奇怪的字符(utf8)【英文标题】:phpmyadmin displaying weird chars (utf8) 【发布时间】:2014-02-12 06:26:09 【问题描述】:

所以我设置了 mysql 以获得完整的 utf8 支持:

我的.cnf:

[mysqld]
character_set_client=utf8
character-set-server=utf8
collation_server=utf8_general_ci

mysql> show variables like "char%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

mysql> show variables like "coll%";
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

现在,我使用 mysql 查询在我的 Web 应用程序中正确存储和获取(和 json_encode)utf8 字符,但我遇到了 phpmyadmin 的问题。问题是 phpmyadmin 将这些国际字符显示为乱码。

έλεος

sadaa

为了澄清一下,我说的是新数据,而不是已经存储的可能已损坏的数据。

phpmyadmin 的会话变量是:

show variables like "char%";
Variable_name   Value   
character_set_client    utf8mb4
character_set_connection    utf8mb4
character_set_database  utf8
character_set_filesystem    binary
character_set_results   utf8mb4
character_set_server    utf8
character_set_system    utf8
character_sets_dir  /usr/share/mysql/charsets/

show variables like "coll%" 
Variable_name   Value   
collation_connection    utf8mb4_general_ci
collation_database  utf8_general_ci
collation_server    utf8_general_ci

到目前为止,我发现国际 utf8 字符在 phpmyadmin 中正确显示而不是乱码的唯一方法是运行

SET NAMES utf8

每次在客户端应用程序 (php) 查询之前: a) 很奇怪,因为你可以看到一切都是正确的 utf8 b)我想避免每次都运行该查询

这是我的设置唯一的 phpmyadmin 问题还是一般数据编码问题?无论哪种方式,我该如何解决这个问题?最好只使用配置文件设置,不必每次都运行 SET NAMES utf8 查询。

提前致谢。

【问题讨论】:

How to display UTF-8 characters in phpMyAdmin? 可能重复。 您忽略了最重要的部分:您的 table 排序规则和您的实际 client 编码。其他一切都只是默认值。如果您一直在没有SET NAMES utf8 的情况下从 PHP 插入数据(尽管这不是推荐的方法),那么它现在可能已经损坏了。 UTF-8 all the way through的可能重复 @ÁlvaroG.Vicario 你是对的。我没有在我的应用程序中设置连接字符集。我相信我现在已经解决了。在下面发布了我自己的答案 【参考方案1】:

回答我自己的问题。

看来不是phpmyadmin的问题,而是一般的编码问题。

问题是我没有设置客户端连接字符集。

如果我从 php 文件中显示变量,我会得到以下结果:

"Variable_name":"character_set_client","Value":"latin1"
"Variable_name":"character_set_connection","Value":"latin1"
"Variable_name":"character_set_database","Value":"utf8"
"Variable_name":"character_set_filesystem","Value":"binary"
"Variable_name":"character_set_results","Value":"latin1"
"Variable_name":"character_set_server","Value":"utf8"
"Variable_name":"character_set_system","Value":"utf8"
"Variable_name":"character_sets_dir","Value":"\/usr\/share\/mysql\/charsets\/"



"Variable_name":"collation_connection","Value":"latin1_swedish_ci"
"Variable_name":"collation_database","Value":"utf8_general_ci"
"Variable_name":"collation_server","Value":"utf8_general_ci"

最初我尝试在 my.cnf 中添加

[client]
default-character-set=utf8

它不起作用。

所以解决方案是添加:

charset=UTF8

在 PDO 初始化中

所以在那之后我得到了正确的变量值:

"Variable_name":"character_set_client","Value":"utf8"
"Variable_name":"character_set_connection","Value":"utf8"
"Variable_name":"character_set_database","Value":"utf8"
"Variable_name":"character_set_filesystem","Value":"binary"
"Variable_name":"character_set_results","Value":"utf8"
"Variable_name":"character_set_server","Value":"utf8"
"Variable_name":"character_set_system","Value":"utf8"
"Variable_name":"character_sets_dir","Value":"\/usr\/share\/mysql\/charsets\/"



"Variable_name":"collation_connection","Value":"utf8_general_ci"
"Variable_name":"collation_database","Value":"utf8_general_ci"
"Variable_name":"collation_server","Value":"utf8_general_ci"

【讨论】:

以上是关于phpmyadmin 显示奇怪的字符(utf8)的主要内容,如果未能解决你的问题,请参考以下文章

phpMyAdmin 不会在数据库中正确显示或插入 Unicode 字符

ajax 发布后的 Phpmyadmin 奇怪的字符

我得到了从 MySQL 数据库中提取数据的奇怪字符

MySQL phpMyAdmin 无效的 utf8mb4 字符串

phpmyadmin 为 mysql utf8_bin 排序列显示数字或 blob?

PhpMyAdmin 正在将 varbinary 导出为奇怪的字符而不是 HEX