In PHP development, character set issues are a common but easily overlooked detail. If the character set is not set correctly, it may cause Chinese garbled code, data storage exceptions, and even trigger security vulnerabilities (such as SQL injection attacks that bypass character set detection).
This article will describe how to write a that automatically detects the currently used character set after connecting to the database and automatically switches to the specified character set when needed to ensure that the application processes multilingual data stably and reliably.
The mysqli extension provides the get_charset() and set_charset() methods:
get_charset() is used to obtain the character set information of the current connection;
set_charset() is used to set the character set used by the connection.
However, in actual use, many developers often forget to check or modify the character set after creating a connection, and directly use the default value (usually latin1 ), which lays hidden dangers for multilingual support.
Encapsulating these two methods into a common connection class can do:
? Automatically detect the current character set
? Automatically switch to the required character set (such as utf8mb4 )
? Provide a concise and consistent interface to reduce duplicate code
Below is a simple DbConnection class implementation that encapsulates mysqli's connection logic and automatically adjusts the character set after connection.
<?php
class DbConnection
{
private $mysqli;
private $host;
private $username;
private $password;
private $database;
private $charset;
public function __construct($host, $username, $password, $database, $charset = 'utf8mb4')
{
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->database = $database;
$this->charset = $charset;
$this->connect();
}
private function connect()
{
$this->mysqli = new mysqli($this->host, $this->username, $this->password, $this->database);
if ($this->mysqli->connect_error) {
die('Connection failed: ' . $this->mysqli->connect_error);
}
// Detect the current character set
$currentCharsetInfo = $this->mysqli->get_charset();
if ($currentCharsetInfo) {
$currentCharset = $currentCharsetInfo->charset;
if (strtolower($currentCharset) !== strtolower($this->charset)) {
// Set to the specified character set
if (!$this->mysqli->set_charset($this->charset)) {
die('Failed to set character set: ' . $this->mysqli->error);
}
}
} else {
die('Unable to obtain the current character set information: ' . $this->mysqli->error);
}
}
public function getConnection()
{
return $this->mysqli;
}
public function close()
{
if ($this->mysqli) {
$this->mysqli->close();
}
}
}
// Example of usage
$db = new DbConnection('localhost', 'db_user', 'db_pass', 'db_name');
// Execute a query
$result = $db->getConnection()->query('SELECT * FROM example_table');
if ($result) {
while ($row = $result->fetch_assoc()) {
print_r($row);
}
$result->free();
}
$db->close();
1?? Constructor receives connection information and target character set <br> The default target character set is utf8mb4 , which is the recommended MySQL character set, which can better support Emoji and multilingual content.
2?? Check the current character set after successful connection <br> Call get_charset() to get the character set used by the current connection.
3?? If different, automatically switch <br> Calling set_charset() changes to the target character set to avoid relying on MySQL's default configuration.
4?? Providing a closing method <br> Actively call close() when no connection is needed to avoid connection leakage.
In a real project, you might want to load database parameters from a configuration file (such as config.php ), or log to a file when an error occurs instead of die() . For example:
error_log('Database error: ' . $this->mysqli->error, 3, '/var/log/db_errors.log');
You can also combine PDO or other database libraries to encapsulate more general methods, such as transactions, preprocessing queries, etc.
If you want to connect to a database with the domain name m66.net , you can write it like this: