Home > Database > Mysql Tutorial > How to Properly Store Emoji Characters in a MySQL Database?

How to Properly Store Emoji Characters in a MySQL Database?

Linda Hamilton
Release: 2024-12-22 17:06:11
Original
434 people have browsed it

How to Properly Store Emoji Characters in a MySQL Database?

Storing Emoji Characters in a MySQL Database

When attempting to store emoji characters in a MySQL database configured with the default utf8mb4_general_ci collation, you may encounter errors such as "Incorrect string value" or "Character set is not valid" due to the storage limitations of certain character sets.

To resolve this issue, follow these steps:

1. Database Collation:

  • Change the default database collation to utf8mb4 by executing the following query:

    ALTER DATABASE `your_database_name` COLLATE utf8mb4_unicode_520_ci;
    Copy after login

2. Table Collation:

  • Change the table collation to utf8mb4_bin by modifying the table structure:

    ALTER TABLE `your_table_name` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
    Copy after login

3. Query:

  • Use the utf8mb4 character set explicitly in your INSERT query:

    INSERT INTO `your_table_name` (column1, column2, column3)
    VALUES ('273', '3', 'Hdhdhdh????hzhzhzzhjzj 我爱你 ❌')
    Copy after login

4. Database Connection:

  • Set the utf8mb4 character set in your database connection:

    $mysqli = new mysqli($server, $username, $password, $database);
    $mysqli->set_charset('utf8mb4');
    Copy after login

By implementing these changes, MySQL will be able to correctly store and retrieve emoji characters within the specified columns.

The above is the detailed content of How to Properly Store Emoji Characters in a MySQL Database?. 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