Home >Database >Mysql Tutorial >Why Are My Emojis Not Saving in My MySQL Database Using utf8mb4?
MySQL utf8mb4: Errors Occurring While Storing Emojis
You encounter errors when attempting to save names containing emojis to your MySQL database using utf8mb4 encoding. The issue stems from differences in the database-specific variables between the global settings and your specific database.
Database Configuration Verification
SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
my.cnf Configuration
Inspect your my.cnf file to ensure the following settings are present:
[mysql] default-character-set = utf8mb4 [mysqld] ... character-set-client-handshake = FALSE character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci
mysqli/PDO Settings
If you're connecting via mysqli/PDO, set the following options:
$mysqli->set_charset('utf8mb4'); $dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8mb4');
Potential Solution
Executing the SQL statement "SET NAMES utf8mb4;" within a MySQL session sets the client, connection, and results character sets to utf8mb4, potentially resolving the issue.
The above is the detailed content of Why Are My Emojis Not Saving in My MySQL Database Using utf8mb4?. For more information, please follow other related articles on the PHP Chinese website!