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

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

Mary-Kate Olsen
Release: 2024-11-26 14:23:10
Original
664 people have browsed it

How to Fix

Incorrect String Value Error with MySQL Emojis

When attempting to store a tweet containing emojis in a MySQL table, an error may occur due to incorrect string value encoding. The error message "Incorrect string value: 'xF0x9Fx8ExB6xF0x9F...' for column 'tweet_text' at row 1." indicates that the character set in use is unable to handle the special characters.

Solution:

To resolve this issue, the database character set must be changed.

  1. Modify MySQL Configuration (my.ini):

    • Open the my.ini file and add the following setting:

      character-set-server=utf8mb4
      Copy after login
  2. Set Character Set and Collation:

    • Connect to MySQL and execute the following commands:

      SET NAMES utf8mb4;
      ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci;
      Copy after login
  3. Verify Changes:

    • Use the following command to confirm that the changes are applied:

      SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
      Copy after login

After completing these steps, the MySQL database will be able to handle the storage of emojis and other special characters in UTF-8 encoding without encountering the "Incorrect string value" error.

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template