php resolves MySQL insert data appears incorrect string value: ' \xf0\x9f\x92\x8bti ... ' ERROR

Source: Internet
Author: User
Tags mysql insert

When inserting data into MySQL in the project, it was found that the data was not inserted completely, and through debugging, it was found that there were no special errors in the INSERT statement. But it's not going to go in, so open the mysqli wrong debug

$ret = Mysqli_query ($this->conn, $sql) or Die (Mysqli_error ($this->conn));


The following error message pops up:

Incorrect string value: ' \xf0\x9f\x92\x8bti ... '


The error message is good to do, the results of online search results are: MySQL encoding format utf-8 format, with four bytes of string insertion is not supported.


Two ways are available online:

1. Upgrade MySQL, then change the corresponding data type to UTF8MB4 type

2. Four bytes of UTF-8 character filtered or converted to a custom type


Because the version of MySQL installed is 5.1, so Method 1 is not suitable; I chose the second filter string and became the appropriate 3 byte utf-8

$str = preg_replace ('/[\x{10000}-\x{10ffff}]/u ', ' ', $str);

Although it is still not a perfect solution, but at least ensure that every piece of data can be inserted correctly!

If you want to solve the problem, it is best to upgrade MySQL, directly change the data type to UTF8MB4 type




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.