In PHP, when using the mysqli extension to operate the database, we can obtain the character set (charset) information of the current database connection through the mysqli::get_charset function. This function returns a mysqli_charset object containing detailed information about the character set, including the name of the character set, the supported character set, and the corresponding collation (character sequence).
This article will show you how to use the mysqli::get_charset function to print and view all fields and details returned.
First, we need to establish a connection to the MySQL database. Suppose you have configured the database and know the host, username, and password of the database.
<?php
$host = 'localhost'; // Database Host
$username = 'root'; // Database username
$password = ''; // Database Password
$dbname = 'test_db'; // Database name
// Create a database connection
$conn = new mysqli($host, $username, $password, $dbname);
// Check if the connection is successful
if ($conn->connect_error) {
die('Connection failed: ' . $conn->connect_error);
}
echo "Connect to the database successfully!<br>";
?>
mysqli::get_charset returns a mysqli_charset object, through which we can obtain various information about the character set. For example, you can obtain the name of the current character set, the support status of the character set, etc.
<?php
// Get the character set information of the current database connection
$charset_info = $conn->get_charset();
// Print character set details
echo "Character set name: " . $charset_info->charset . "<br>";
echo "Default proofreading rules for character sets: " . $charset_info->collation . "<br>";
echo "Character set ranges supported by character sets: " . $charset_info->supported . "<br>";
?>
The mysqli_charset object contains multiple fields that you can view detailed information about the database character set by accessing them. We can print out the contents of these fields to give a clearer understanding of the character set configuration.
<?php
// Get character set details
$charset_details = $conn->get_charset();
// Print all relevant field information for the character set
echo "<pre>";
print_r($charset_details);
echo "</pre>";
?>
Use the print_r() function to print out the detailed content of the object, helping us better understand the structure of the object and the fields returned.
After getting and printing the character set, we should close the database connection to free up the resource.
<?php
// Close the database connection
$conn->close();
?>
Through the mysqli::get_charset function, we can easily obtain the character set and related information of the current database connection. This is useful for debugging and viewing the database's character set configuration, especially when you need to make sure the database uses the correct character set.
If you need to have a deeper understanding of the character set of a database in actual development, or switch character sets between multiple databases, you can refer to the above code for details.