In multilingual site development, character encoding management is a very important issue. Different languages and regions may use different character sets, and if character encoding is not set correctly, the content in the site may appear garbled or cannot be displayed correctly. PHP provides a mysqli extension to operate a MySQL database. In this extension, the mysqli::get_charset function can help us get the character set of the current database connection.
In this article, we will discuss how to use the mysqli::get_charset function to uniformly manage character encoding and ensure that the content in different languages is displayed correctly in a multilingual site.
Character encoding is the way to map characters to a sequence of bytes. In multilingual sites, it is important to set up the correct character encoding, especially when the site supports multiple languages. Common character encodings include UTF-8, ISO-8859-1, GBK, etc. UTF-8 is a widely used character encoding that supports characters in almost all languages.
In multilingual sites, different languages may use different character sets. If we do not have unified character encoding management, we may encounter the following problems:
The content displays garbled code
Characters are corrupted when the database is stored
The content displayed on the page is inconsistent
To ensure that the content is displayed correctly between different languages, it is necessary to ensure that the database and front-end pages use the same character encoding.
In PHP, the mysqli extension provides a convenient interface to connect and operate MySQL databases. The mysqli::get_charset function returns the character set of the current database connection. We can use it to ensure that the character encoding of the database connection and the page is consistent, thereby avoiding the garbled problem.
When connecting to a MySQL database, we need to explicitly set the character encoding to UTF-8 to ensure that characters in all languages are supported. You can do it with the following code:
<?php
// create MySQLi Database connection
$mysqli = new mysqli("localhost", "username", "password", "database_name");
// Check if the connection is successful
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Set the character set to UTF-8
if (!$mysqli->set_charset("utf8")) {
printf("mistake:Unable to load character set utf8: %s\n", $mysqli->error);
exit();
}
// Check the currently connected character set
echo "Current character set: " . $mysqli->get_charset()->charset;
?>
The above code completes the following tasks:
Connect to the database.
Set the character set to UTF-8 to ensure that all inputs and outputs are encoded using UTF-8.
Use mysqli::get_charset to get the current character set and output it.
After ensuring that the database characters are encoded as UTF-8, we also need to make sure that the front-end pages also use the same encoding. You can specify character encoding through meta tags in the HTML page, as shown below:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multilingual site</title>
</head>
<body>
<h1>欢迎访问我们的Multilingual site!</h1>
<!-- Other page content -->
</body>
</html>
Here, meta charset="UTF-8" is used to tell the browser page to use UTF-8 character encoding, so as to ensure that content in different languages such as Chinese and English can be displayed correctly.
In multilingual sites, the corresponding content is usually obtained from the database based on the language selected by the user. If the character encoding of the database and the page is consistent, there will be no problem getting and displaying these contents.
For example, we can use the following code to get multilingual content from the database:
<?php
// Assumptions $mysqli It's already connected MySQLi Example
// Get Chinese page content
$query = "SELECT title, content FROM pages WHERE language = 'zh' LIMIT 1";
$result = $mysqli->query($query);
if ($result) {
$row = $result->fetch_assoc();
echo "<h1>" . htmlspecialchars($row['title']) . "</h1>";
echo "<p>" . nl2br(htmlspecialchars($row['content'])) . "</p>";
} else {
echo "Query failed: " . $mysqli->error;
}
?>
In this example, the htmlspecialchars function is used to safely output content from the database to the page, preventing XSS attacks.
By using mysqli::get_charset and mysqli::set_charset , we can ensure that character encoding is managed uniformly in multilingual sites, avoiding garbled and display issues. Remember to set the correct character set when connecting to the database and make sure the character encoding of the front-end page is consistent. This way, users can see the correct content no matter which language they are visiting the site.