MySQL可以选择litral表情符号,但不会将表情符号存储到表中

时间:2015-07-07 06:22:50

标签: mysql encoding utf8mb4

我试图在MySQL的表中插入一些表情符号字符,但值存储为问号(????)。

我确保使用正确的utf8mb4编码创建数据库:

enter image description here enter image description here

mysql> describe users;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(191) | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)

然后我试着确定MySql是否理解表情符号,所以我这样做了:

mysql> select '';

+------+
|      |
+------+
|      |
+------+
1 row in set (0.00 sec)

然后我这样做了:

mysql> insert into users (name) values ('');
Query OK, 1 row affected, 1 warning (0.05 sec)
mysql> select * from users;
+----+------------+
| id | name       |
+----+------------+
| 21 | فاضل       |
| 30 | سلاحف      |
| 46 | ????       |
| 47 | ????       |
| 48 | ????       |
| 49 | ????       |
+----+------------+
6 rows in set (0.01 sec)

我不知道如何解决这个问题......

**编辑**:根据评论的要求,我运行了以下命令:

mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+-------------------------+
| Variable_name            | Value                   |
+--------------------------+-------------------------+
| character_set_client     | utf8                    |
| character_set_connection | utf8                    |
| character_set_database   | utf8mb4                 |
| character_set_filesystem | binary                  |
| character_set_results    | utf8                    |
| character_set_server     | utf8                    |
| character_set_system     | utf8                    |
| character_sets_dir       | /static/share/charsets/ |
+--------------------------+-------------------------+
8 rows in set (0.00 sec)

1 个答案:

答案 0 :(得分:1)

您的连接已设置为utf8;它需要设置为utf8mb4。

你是怎么设置的?更改为适用于这些中的任何一个。

  • SET NAMES utf8mb4
  • PDO(... charset=utf8mb4)
  • mysqli::set_charset('utf8mb4')

表情符号是4字节的utf8代码,因此四个问号。