Current Location: Home> Latest Articles> How to detect the current character set immediately after connecting to the database?

How to detect the current character set immediately after connecting to the database?

M66 2025-05-26

In PHP, the mysqli extension provides us with the function of manipulating MySQL databases. When connecting to a database using mysqli , a character set is set by default. The character set determines how the database processes different characters when reading and writing data. To ensure the correctness of the data, it is important to view and manage the character set.

This article will introduce how to use the mysqli::get_charset function to view the character set used by the current database connection immediately after connecting to the database.

1. Connect to the database

First, we need to connect to the MySQL database through the mysqli class. Suppose you already have the host name, user name, password, and database name of the database. You can use the following code to connect to the database:

 <?php
$host = 'localhost'; // Database Host
$user = 'root'; // Database username
$password = ''; // Database Password
$dbname = 'testdb'; // Database name

// Create a newmysqliObject,Connect to the database
$mysqli = new mysqli($host, $user, $password, $dbname);

// Check if the connection is successful
if ($mysqli->connect_error) {
    die('Failed to connect to the database: ' . $mysqli->connect_error);
}
echo 'Database connection is successful!';
?>

2. Set character set

After connecting to the database, we may need to set up the character set to ensure the correct encoding of the data. For example, we can set the character set to utf8mb4 , which is a Unicode character set that supports more characters. You can use the following code to set the character set:

 <?php
// Set the character set toutf8mb4
if (!$mysqli->set_charset("utf8mb4")) {
    printf("Unable to load character set utf8mb4: %s\n", $mysqli->error);
    exit();
}
echo 'The character set has been set toutf8mb4';
?>

3. Use mysqli::get_charset to view the current character set

After connecting and setting the character set, we can use the mysqli::get_charset method to view the character set used by the current connection. This method returns a mysqli_charset object containing the current character set information. You can get the character set name by accessing the charset property of the object.

 <?php
// Get the currently used character set
$charset_info = $mysqli->get_charset();

// Output the current character set
echo 'The current character set is: ' . $charset_info->charset;
?>

When running the above code, $charset_info->charset will return the character set used by the current connection. If you have set a character set before this, it will display the character set you set, such as utf8mb4 .

4. Complete code example

Combine all the previous steps, and the final complete code looks like this:

 <?php
$host = 'localhost'; // Database Host
$user = 'root'; // Database username
$password = ''; // Database Password
$dbname = 'testdb'; // Database name

// Create a newmysqliObject,Connect to the database
$mysqli = new mysqli($host, $user, $password, $dbname);

// Check if the connection is successful
if ($mysqli->connect_error) {
    die('Failed to connect to the database: ' . $mysqli->connect_error);
}
echo 'Database connection is successful!<br>';

// Set the character set toutf8mb4
if (!$mysqli->set_charset("utf8mb4")) {
    printf("Unable to load character set utf8mb4: %s\n", $mysqli->error);
    exit();
}
echo 'The character set has been set toutf8mb4<br>';

// Get the currently used character set
$charset_info = $mysqli->get_charset();

// Output the current character set
echo 'The current character set is: ' . $charset_info->charset;
?>

5. Conclusion

Use the mysqli::get_charset function to easily view the character set used by the database connection. Ensuring that the character set is correctly set is very important to avoid garbled data and ensure the correctness of the data. By checking the character set immediately after connecting to the database, we can ensure that our application is able to process data from different languages ​​and character sets.