In PHP development, Chinese characters garbled are a common problem when handling database and front-end data transmission. Especially when using mysqli::get_charset and json_encode , if the character set is not configured correctly, it may lead to garbled Chinese content. This article will explain how to resolve this problem and ensure that Chinese characters can be passed correctly when interacting with a MySQL database.
First, make sure that the character set is correctly set to UTF-8 when the database connection is connected so that MySQL can handle Chinese characters correctly. We use the mysqli extension to connect to the database, and then use mysqli::get_charset to get the current character set settings. Ensuring that the character set is UTF-8 is a key step in solving the garbled problem.
<?php
// Connect to the database
$mysqli = new mysqli('localhost', 'root', '', 'test_db');
// Check if the connection is successful
if ($mysqli->connect_error) {
die('Connection failed: ' . $mysqli->connect_error);
}
// Set the character set to UTF-8
$mysqli->set_charset('utf8');
// Get the current character set
$current_charset = $mysqli->get_charset();
echo "Current character set: " . $current_charset->charset;
// Perform database query and output Chinese data
$query = "SELECT name FROM users WHERE id = 1";
$result = $mysqli->query($query);
$row = $result->fetch_assoc();
// Output query results
echo json_encode($row);
?>
When we query Chinese data from the database and encode it through json_encode , if the character set is not set correctly, Chinese characters may be garbled. To avoid this, we can use the JSON_UNESCAPED_UNICODE parameter to ensure that Chinese characters are not escaped.
<?php
// Connect to the database
$mysqli = new mysqli('localhost', 'root', '', 'test_db');
// Check if the connection is successful
if ($mysqli->connect_error) {
die('Connection failed: ' . $mysqli->connect_error);
}
// Set the character set to UTF-8
$mysqli->set_charset('utf8');
// Get the current character set
$current_charset = $mysqli->get_charset();
echo "Current character set: " . $current_charset->charset;
// Perform database query and output Chinese data
$query = "SELECT name FROM users WHERE id = 1";
$result = $mysqli->query($query);
$row = $result->fetch_assoc();
// use json_encode Output Chinese,Make sure Chinese characters are not escaped
echo json_encode($row, JSON_UNESCAPED_UNICODE);
?>
If the database table character set you are using is not UTF-8, you may encounter character conversion problems. To ensure that there is no garbled code encountered throughout the development environment, it is recommended to check the following items:
Character set of database: Use utf8mb4 or utf8 .
Character set of data tables: The following SQL queries can be used to view and modify the character set of a table:
SHOW CREATE TABLE users;
ALTER TABLE users CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Connected character set: In PHP, set the character set by mysqli->set_charset('utf8') .
In addition to the above configuration, it is also key to ensure that the browser can also correctly decode Chinese data. A common practice is to add the following meta tag to the <head> tag of a web page:
<meta charset="UTF-8">
This ensures that the browser correctly parses Chinese characters and avoids garbled code.
When using mysqli::get_charset with json_encode for Chinese character processing, correctly configuring the character set is the key to avoiding garbled code problems. Make sure that the database connection character set is set to utf8 or utf8mb4 , and add the JSON_UNESCAPED_UNICODE parameter when using json_encode , which can effectively avoid the problem of garbled Chinese.
Through the introduction of this article, it should help you avoid the troubles caused by Chinese garbled code when using mysqli and json_encode .