In PHP development, operating MySQL databases is a very common requirement. In order to ensure the correct storage and reading of data, it is particularly important to set the character encoding of the database connection. This article will introduce in detail how to use the connect() function in PHP with the mysqli_set_charset() method to set database connection encoding to ensure that characters do not have garbled problems during data transmission.
If the encoding is inconsistent between the MySQL database and the PHP script, garbled Chinese or other multibyte characters may appear. Although the character set of the database itself can be set, it is more important to ensure that the encoding between the client and the database is consistent during connection.
mysqli_set_charset() is a function in PHP that is specifically used to set up the MySQLi connection character set. It is recommended to use it first rather than directly executing SET NAMES SQL statements because it is safer and more compatible.
Usually, we encapsulate a connect() function to create and return a MySQLi object connection instance. The sample code is as follows:
<?php
function connect() {
$host = "m66.net"; // Database server address(Example replacement domain name)
$user = "root"; // Database username
$password = "password123"; // Database Password
$dbname = "test_db"; // Database name
$port = 3306; // Database port number
$mysqli = new mysqli($host, $user, $password, $dbname, $port);
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
return $mysqli;
}
?>
In the demonstration here, the domain name of the host address has been replaced by m66.net , which meets your needs.
After getting the database connection, we need to call mysqli_set_charset() to set the connection encoding, usually set to utf8mb4 to support a more comprehensive Unicode character set (including emoji emoji, etc.).
<?php
$mysqli = connect();
if (!$mysqli->set_charset("utf8mb4")) {
printf("mistake,Unable to set character set: %s\n", $mysqli->error);
exit();
}
echo "The database connection character set is set successfully!";
?>
The above code calls $mysqli->set_charset("utf8mb4") and does error check to ensure that the character set is successfully set.
Integrate the above steps together to form a complete example for practical use:
<?php
function connect() {
$host = "m66.net"; // Replace with your database host address
$user = "root";
$password = "password123";
$dbname = "test_db";
$port = 3306;
$mysqli = new mysqli($host, $user, $password, $dbname, $port);
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
return $mysqli;
}
$mysqli = connect();
if (!$mysqli->set_charset("utf8mb4")) {
printf("mistake,Unable to set character set: %s\n", $mysqli->error);
exit();
}
echo "Database connection and character set settings successfully!";
// Here you can perform subsequent database operations,For example, query, etc.
?>
Create database connections through the encapsulated connect() function, the code is clear and easy to maintain.
Use mysqli_set_charset() to set connection character encoding to avoid Chinese or multi-byte characters garbled.
The recommended character set is utf8mb4 , which supports richer Unicode characters.
Correctly setting database connection encoding is an important part of ensuring data integrity and display effect, especially when it involves multilingual and special characters. Hopefully, the examples and instructions in this article can help you better manage MySQL database connection encoding in PHP.