php resolves MySQL insert data appears incorrect String value: & #39;\xf0\x9f\x92\x8bti...& #39; error

Source: Internet
Author: User
Tags format insert mysql mysql in mysql insert string

When inserting data into MySQL in a project, it is found that data insertions are incomplete and there is no special error in inserting statements through debugging. But just don't go in, and then open mysqli error debugging

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


The result pops up the following error message:

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


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


Online offers two ways:

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

2. The four-byte UTF-8 character that appears filtered or converted to a custom type


Since the version of MySQL installed is 5.1, so Method 1 is not suitable; I chose the second filter string to fit into the 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 each piece of data can be inserted correctly!

If you want to perfect solution, it is best to upgrade MySQL, directly change the data type for 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.