php mysql 存储 IOS Emoji表情失败和乱码问题
感谢伟大的网络,给我们带来了很多有用的信息
然后开始 项目需要给app提供接口,发现IOS开发存储Emoji表情失败,或者乱码,在这里给自己留个记录以及给大家带来一些参考
首先是字符集的问题,mysql字符集大多数使用的是utf8,需要把字符集更换成utf8mb4,因为UTF-8编码有可能是两个、三个、四个字节。Emoji表情是4个字节,而Mysql的utf8编码最多3个字节,utf8mb4则扩展到一个字符最多能有4字节,并且兼容utf8
在mysql终端查看下mysql当前版本所支持的字符集,据说mysql 5.5.3版本上才会支持utf8mb4,我没核对;
1 show charset;
1. 修改database,table,column字符集mysql语句如下
# For each database: ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci; # For each table: ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; # For each column: ALTER TABLE table_name CHANGE column_name column_name VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; # (Don’t blindly copy-paste this! The exact statement depends on the column type, maximum length, and other properties. The above line is just an example for a `VARCHAR` column.)
2. 修改my.cnf
[client] default-character-set = utf8mb4 [mysql] default-character-set = utf8mb4 [mysqld] character-set-client-handshake = FALSE character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci init_connect=‘SET NAMES utf8mb4‘
3.重启mysql,检查字符集
mysql> SHOW VARIABLES WHERE Variable_name LIKE ‘character\_set\_%‘ OR Variable_name LIKE ‘collation%‘;
+--------------------------+--------------------+
| Variable_name | Value |
+--------------------------+--------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| collation_connection | utf8mb4_unicode_ci |
| collation_database | utf8mb4_unicode_ci |
| collation_server | utf8mb4_unicode_ci |
+--------------------------+--------------------
这样mysql数据库就支持Emoji表情的存储了
如果你的php项目存储Emoji表情会出现乱码,那么把你php连接Mysql数据库的字符编码换成 utf8mb4
另外Mariadb数据库同样可以这样设置
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。