Current Location: Home> Latest Articles> Use mysqli::get_charset to check whether non-English characters are processed correctly

Use mysqli::get_charset to check whether non-English characters are processed correctly

M66 2025-05-25

When using PHP to operate MySQL databases, character encoding issues are often one of the most troublesome problems for developers, especially when it involves non-English characters such as Chinese, Japanese, and Korean. Many people encounter Chinese garbled code after inserting or querying the database, which is often because the character set is not set correctly.

This article will explain how to use the mysqli::get_charset method to check the character set settings of the current MySQLi connection to ensure that non-English characters (such as Chinese) can be correctly encoded.

Why are character sets important?

MySQL supports multiple character sets, such as latin1 , utf8 , utf8mb4 , etc. If your website contains Chinese content, it is recommended to use utf8mb4 because it is compatible with Emoji and almost all Unicode characters.

But setting a character set at the database level is not enough, and you must also set a consistent character set in the connection between PHP and the database. Otherwise, the data sent to the database may have been incorrectly encoded before it arrives, resulting in garbled code.

What is the use of mysqli::get_charset

The mysqli::get_charset method can be used to view the character set information used by the current connection, and returns an object containing the character set details. With this method, you can write a detection code to ensure that the connection is set correctly.

Sample code

 <?php
// Database connection configuration
$host = 'localhost';
$user = 'dbuser';
$password = 'dbpassword';
$database = 'testdb';

// create mysqli Object
$mysqli = new mysqli($host, $user, $password, $database);

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

// Set the character set to utf8mb4(Recommended support Chinese and Emoji)
if (!$mysqli->set_charset('utf8mb4')) {
    die('Error setting character set: ' . $mysqli->error);
}

// use get_charset Check the current character set
$charset_info = $mysqli->get_charset();

echo "Current character set: " . $charset_info->charset . "<br>";
echo "Character Set Description: " . $charset_info->comment . "<br>";
echo "Character Set Directory: " . $charset_info->dir . "<br>";
echo "Maximum byte length: " . $charset_info->max_length . "<br>";

// Test insert Chinese data
$sql = "INSERT INTO test_table (content) VALUES ('Test Chinese characters')";
if ($mysqli->query($sql)) {
    echo "Successfully inserted Chinese data。<br>";
} else {
    echo "Insert failed: " . $mysqli->error . "<br>";
}

// Test reading data from the database
$result = $mysqli->query("SELECT content FROM test_table ORDER BY id DESC LIMIT 1");
if ($result) {
    $row = $result->fetch_assoc();
    echo "Read content: " . $row['content'] . "<br>";
} else {
    echo "Query failed: " . $mysqli->error . "<br>";
}

// Close the connection
$mysqli->close();
?>

Things to note

  1. The database table and field character set should be consistent <br> Make sure that the character set of your table and fields is also utf8mb4 , you can use SQL as follows:

     SHOW CREATE TABLE test_table;
    

    If not, you can change to a consistent character set via ALTER TABLE .

  2. Test page coding <br> Make sure your PHP page itself is saved with UTF-8 encoding and that the correct Content-Type is declared in the HTTP response header:

     header('Content-Type: text/html; charset=utf-8');
    
  3. Use mysqli_report to facilitate debugging <br> MySQLi reporting function can be enabled during development to facilitate catching hidden errors:

     mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);