php 解决MySQL插入数据出现 Incorrect string value: 'xF0x9Fx92x8BTi...'错误
php 解决MySQL插入数据出现 Incorrect string value: '\xF0\x9F\x92\x8BTi...'错误
在项目中向MySQL插入数据时,发现数据插入不完整,通过调试,发现插入语句也没什么特殊的错误。但是就是差不进去,于是就打开mysqli错误的调试
$ret = mysqli_query($this->conn, $sql) or die(mysqli_error($this->conn));
Incorrect string value: '\xF0\x9F\x92\x8BTi...'
有错误信息就好办了,结果上网一查结果是:mysql编码格式utf-8格式,不支持带四字节的字符串插入。
网上提供两种方法:
1.升级MySQL,然后将对应的数据类型改为utf8mb4类型
2.出现的四字节UTF-8字符过滤或转化为自定义类型
由于装的MySQL版本为5.1,所以方法1不适合;本人选了第二种过滤字符串,变成合适的3字节utf-8
$str = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $str);
虽然还是不能很完美解决,但是至少保证每条数据都可以正确插入!
如果想完美解决的话,最好升级MySql,直接改数据类型为utf8mb4类型

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools