简体   繁体   English

MySQL插入字符串错误 - UTF-8?

[英]MySQL insert String error - UTF-8?

I am inserting into a mysql database. 我正在插入一个mysql数据库。 I get the following error when trying to do the insert 尝试插入时出现以下错误

Incorrect string value: '\xF0\x9F\x87\xB7\xF0\x9F...' for column 'field_4' at row 1

I thought I had figured out this error by simply changing the column encoding the to utf8mb4 and had tested but recently this error appeared again. 我以为我只是通过简单地将列编码更改为utf8mb4来解决这个错误并且已经测试但最近又出现了这个错误。 I am using php to parse the string and run the following function before inserting... 我在使用php来解析字符串并在插入之前运行以下函数...

function strip_emoji($subject) {
    if (is_array($subject)) {
        // Recursive strip for multidimensional array
        foreach ($subject as &$value) $value = $this->strip_emoji($value);
        return $subject;
    } else {
        // Match Emoticons
        $regexEmoticons = '/[\x{1F600}-\x{1F64F}]/u';
        $clean_text = preg_replace($regexEmoticons, '', $subject);

        // Match Miscellaneous Symbols and Pictographs
        $regexSymbols = '/[\x{1F300}-\x{1F5FF}]/u';
        $clean_text = preg_replace($regexSymbols, '', $clean_text);

        // Match Transport And Map Symbols
        $regexTransport = '/[\x{1F680}-\x{1F6FF}]/u';
        $clean_text = preg_replace($regexTransport, '', $clean_text);
        return 
}

There are several similar questions to this but I still have these errors. 有几个类似的问题,但我仍然有这些错误。 Any further advice on how to prevent this error? 有关如何防止此错误的任何进一步建议? I realize that it is an emoji unicode character / sprite but not sure how to deal with it. 我意识到它是一个表情符号unicode字符/精灵但不知道如何处理它。

You are trying to insert a character that spans 4 bytes, so you have to convert the column to the utf8mb4 character set. 您正在尝试插入跨越4个字节的字符,因此您必须将列转换为utf8mb4字符集。

The utf8 character set is limited to characters that span 3 bytes (the Unicode characters U+0000 through U+FFFF). utf8字符集限制为跨越3个字节的字符(Unicode字符U + 0000到U + FFFF)。

Do you have utf8 charset for the connection as well? 你有连接的utf8字符集吗?

Adding ";charset=utf8" in the PDO-connection string , or executing the query " set names utf8 ". PDO连接字符串中添加“; charset = utf8”,或执行查询“ set names utf8 ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM