报错信息 : Incorrect string value: '\xF0\x9F\x99\x85\xE2\x80...' for column 'content' at row 1 

mysql数据库的默认字符集utf8,只能存储3个字节的数据,标准的emoji表情是4个字节,所以要使用utf8mb4兼容四个字节

解决办法

1. 将表字段字符集设置成utf8mb4 

修改库的字符集

alter database 库名 character set utf8mb4 collate utf8mb4_general_ci


修改表的字符集

alter table 表名 character set utf8mb4 collate utf8mb4_general_ci

或者ALTER TABLE 表名 MODIFY COLUMN content varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '备注';

 


2. 执行插入前执行:SET NAMES utf8mb4; 
如: 
SET NAMES utf8mb4; 
INSERT test(Content) VALUES('~');

PHP例子: 
$paramValues=array('Content'=>'~'); 
$dbCommand->query('SET NAMES utf8mb4'); 
$lastInsertID=$dbCommand->insertOne('test',$paramValues);

 

相关文章:

  • 2022-01-16
  • 2021-06-27
  • 2021-11-25
  • 2021-05-13
  • 2022-12-23
  • 2021-08-15
  • 2021-09-24
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-22
  • 2021-12-10
  • 2021-12-24
  • 2022-12-23
  • 2021-08-08
  • 2022-12-23
相关资源
相似解决方案