Home >Database >Mysql Tutorial >How to Resolve \'Incorrect String Value\' Errors When Storing Emojis in MySQL?

How to Resolve \'Incorrect String Value\' Errors When Storing Emojis in MySQL?

Susan Sarandon
Susan SarandonOriginal
2024-11-22 05:49:14637browse

How to Resolve

Incorrect String Value: Encoding Resolution for Emoji and Emoticon Storage

While attempting to store a tweet containing special UTF-8 characters into a MYSQL table, an error message - "Incorrect string value: 'xF0x9Fx8ExB6xF0x9F...'" - arises. This error occurs despite the table's 'tweet_text' field being encoded in utf8mb4, which should support multiple languages.

After investigating the cause, it was determined that necessary MYSQL configuration settings were not correctly configured. To resolve the issue:

  1. Edit the mysql configuration file (my.ini): Change the character set to utf8mb4.
  2. Connect to the MYSQL client and execute the following commands:

    • SET NAMES utf8mb4;
    • ALTER DATABASE CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci;
  3. Verify the changes: Run the following command to confirm the character set and collation have been updated:

    • SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';

After completing these steps, the error message should no longer persist, and the tweet, including its 'MULTIPLE MUSICAL NOTES' characters, will be successfully stored in the 'tweet_text' column.

The above is the detailed content of How to Resolve 'Incorrect String Value' Errors When Storing Emojis in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn