Current Location: Home> Latest Articles> Make sure the character set is consistent with the MySQL configuration with mysqli::character_set_name

Make sure the character set is consistent with the MySQL configuration with mysqli::character_set_name

gitbox 2025-05-29

What is mysqli::character_set_name ?

mysqli::character_set_name is a method in PHP's mysqli extension that returns the character set name used by the current database connection. The prototype of this method is as follows:

 string mysqli::character_set_name ( void )

This means you can call it before performing the SQL operation to confirm that the character set used by the connection is consistent with your database configuration.


Why is character set consistency important?

MySQL supports multiple character sets, such as utf8 , utf8mb4 , latin1 , etc. Inconsistent character sets can lead to the following problems:

  • Chinese characters are garbled or cut off

  • Unable to execute query containing multilingual content correctly

  • An exception occurred after the data is written to the database to read again

Problems are particularly common, especially when MySQL's default character set does not match the character set used by PHP. Therefore, it is a good practice to verify the character set in time after the connection is established.


How to use mysqli::character_set_name ?

Here is a complete example for detecting and confirming the character set settings of the currently connected:

 <?php
$mysqli = new mysqli("localhost", "user", "password", "database");

// Check if the connection is successful
if ($mysqli->connect_errno) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Get the character set of the currently connected
$currentCharset = $mysqli->character_set_name();

echo "The character set used by the current connection is: " . $currentCharset;

// Check if it is the expected character set,for example utf8mb4
if ($currentCharset !== 'utf8mb4') {
    echo "Character set inconsistent,Setting as utf8mb4...";
    if (!$mysqli->set_charset("utf8mb4")) {
        die("Failed to set character set: " . $mysqli->error);
    }
    echo "The character set has been updated to: " . $mysqli->character_set_name();
}
?>

After this example is connected to the database, it first outputs the currently used character set. If it is not utf8mb4 , try resetting the character set. By cooperating with mysqli::set_charset and mysqli::character_set_name , ensure that the character set is completely consistent.


Extension of configuration consistency: Reading the default character set from the MySQL configuration file

If you want to make sure that the character set used by PHP is exactly the same as the default character set on the server side, consider reading the default settings from MySQL's configuration file (usually my.cnf or my.ini ).

For example, you can use the following SQL query to view the server-side default character set settings:

 <?php
$result = $mysqli->query("SHOW VARIABLES LIKE 'character_set_server'");
$row = $result->fetch_assoc();
$serverCharset = $row['Value'];

echo "MySQLThe default character set of the server is: " . $serverCharset;

if ($mysqli->character_set_name() !== $serverCharset) {
    echo "检测到Character set inconsistent,Trying to sync...";
    if (!$mysqli->set_charset($serverCharset)) {
        die("Failed to set character set: " . $mysqli->error);
    }
    echo "The character set has been synchronized as: " . $mysqli->character_set_name();
}
?>

This code not only checks the currently connected character set, but also compares it with the server's default character set, automatically synchronizes the character set settings to further ensure data consistency.