教程集 www.jiaochengji.com
教程集 >  数据库  >  mysql  >  正文 mysql如何存储emoji表情

mysql如何存储emoji表情

发布时间:2023-04-29   编辑:jiaochengji.com
教程集为您提供mysql如何存储emoji表情等资源,欢迎您收藏本站,我们将为您提供最新的mysql如何存储emoji表情资源
在mysql中如何存储emoji表情,如果mysql是utf-8编码,存入表情符出错我们如何解决,下面我们来介绍一下。

utf8的数据库,存入表情符,会出错

 代码如下 复制代码
Incorrect string value: '\xF0\x9F\x98\x84\xF0\x9F...' for column 'content'




错误的解决办法:

 代码如下 复制代码

4 byte Unicode characters aren't yet widely used, so not every application out there fully supports them. MySQL 5.5 works fine with 4 byte characters when properly configured – check if your other components can work with them as well.

Here's a few other things to check out:

Make sure all your tables' default character sets and text fields are converted to utf8mb4, in addition to setting the client & server character sets, e.g. ALTER TABLE mytable charset=utf8mb4, MODIFY COLUMN textfield1 VARCHAR(255) CHARACTER SET utf8mb4,MODIFY COLUMN textfield2 VARCHAR(255) CHARACTER SET utf8mb4; and so on.

If your data is already in the utf8 character set, it should convert to utf8mb4 in place without any problems. As always, back up your data before trying!

Also make sure your app layer sets its database connections' character set to utf8mb4. Double-check this is actually happening – if you're running an older version of your chosen framework's mysql client library, it may not have been compiled with utf8mb4 support and it won't set the charset properly. If not, you may have to update it or compile it yourself.

When viewing your data through the mysql client, make sure you're on a machine that can display emoji, and run a SET NAMES utf8mb4 before running any queries.

Once every level of your application can support the new characters, you should be able to use them without any corruption.




总结就是,表结构改为支持4字节的unicode,数据库连接也用这个字符集哦,证明是可行的。
如果别的地方不支持,可以考虑去掉这些字符:

 代码如下 复制代码
Since 4-byte UTF-8 sequences always start with the bytes 0xF0-0xF7, the following should work:

$str = preg_replace('/[\xF0-\xF7].../s', '', $str);
Alternatively, you could use preg_replace in UTF-8 mode but this will probably be slower:

$str = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $str);
This works because 4-byte UTF-8 sequences are used for code points in the supplementary Unicode planes starting from 0x10000.

您可能感兴趣的文章:
PHP 开发中涉及到emoji表情的几种处理方法
mysql5.5.3以上版本支持存储emoji
mysql中字符集 utf8 和utf8mb4 有什么区别?
简单明了!utf8和utf8mb4的区别
mysql db字符集升级至utf8mb4的方案
MySQL乱码问题以及utf8mb4字符集
大话Python的垃圾回收机制
分享:Mysql 5.0存储过程学习总结
django如何处理表单数据
怎么查看mysql数据库的引擎

[关闭]
~ ~