Current Location: Home> Latest Articles> Practical Example of Using mysqli::character_set_name to Retrieve Character Set and Perform Encoding Conversion

Practical Example of Using mysqli::character_set_name to Retrieve Character Set and Perform Encoding Conversion

gitbox 2025-09-08

<?php // Example PHP article generator // Article Title: Practical Example of Using mysqli::character_set_name to Retrieve Character Set and Perform Encoding Conversion // Output the article, with the body and preface separated by a horizontal line echo <<<EOT Practical Example of Using mysqli::character_set_name to Retrieve Character Set and Perform Encoding Conversion ---------------------------------------------------------------------

When using PHP’s mysqli extension to interact with a MySQL database, character set handling is a very common and critical concern. If not handled properly, issues such as garbled Chinese characters, inconsistencies between data storage and retrieval, and display errors often occur. This article introduces how to use the `mysqli::character_set_name` method to obtain the character set currently used by the connection, and how to combine it with encoding conversion to ensure data integrity.

1. Introduction to mysqli::character_set_name

The `mysqli::character_set_name` method returns the character set currently in use for the database connection. For example, if the connection’s character set is set to UTF-8, this method will return “utf8” or “utf8mb4.” This makes it easy to check whether the current connection is using the expected character set.

2. Practical Example

Below is a simple code example demonstrating how to use `mysqli::character_set_name` to retrieve the character set, and perform encoding conversion if necessary.

&lt;?php
// Create database connection
</span></span><span><span class="hljs-subst">$mysqli</span></span><span> = new mysqli("localhost", "username", "password", "test_db");

// Check connection
if (</span><span><span class="hljs-subst">$mysqli</span></span><span>-&gt;connect_errno) {
    die("Database connection failed: " . </span><span><span class="hljs-subst">$mysqli</span></span><span>-&gt;connect_error);
}

// Set character set
</span><span><span class="hljs-subst">$mysqli</span></span><span>-&gt;set_charset("utf8mb4");

// Get current character set
</span><span><span class="hljs-subst">$current_charset</span></span><span> = </span><span><span class="hljs-subst">$mysqli</span></span><span>-&gt;character_set_name();
echo "The current connection character set is: " . </span><span><span class="hljs-subst">$current_charset</span></span><span> . "\\n";

// Query data from the database
</span><span><span class="hljs-subst">$result</span></span><span> = </span><span><span class="hljs-subst">$mysqli</span></span><span>-&gt;query("SELECT name FROM users LIMIT 1");
</span><span><span class="hljs-subst">$row</span></span><span> = </span><span><span class="hljs-subst">$result</span></span><span>-&gt;fetch_assoc();

// If the database encoding differs from the page display encoding, convert as needed
// For example, convert utf8mb4 data to GBK for output
</span><span><span class="hljs-subst">$name</span></span><span> = </span><span><span class="hljs-subst">$row</span></span><span>[&#039;name&#039;];
</span><span><span class="hljs-subst">$converted_name</span></span><span> = iconv("UTF-8", "GBK//IGNORE", </span><span><span class="hljs-subst">$name</span></span><span>);

echo "Original data: " . </span><span><span class="hljs-subst">$name</span></span><span> . "\\n";
echo "Converted data: " . </span><span><span class="hljs-subst">$converted_name</span></span><span> . "\\n";

</span><span><span class="hljs-subst">$mysqli</span></span><span>-&gt;close();
?&gt;
</span></span>

  1. Set the character set first: Always call `set_charset` immediately after connecting to the database to ensure consistent behavior.

  2. Avoid garbled text issues: The frontend page encoding should match the database encoding; otherwise, use `iconv` or `mb_convert_encoding` to convert.

  3. Use utf8mb4 whenever possible: Compared with utf8, `utf8mb4` provides full Unicode support, including emojis.

4. Conclusion

With `mysqli::character_set_name`, developers can easily confirm the character set used by the current connection. Combined with encoding conversion functions, this ensures data consistency during storage and display. Proper character set management not only prevents garbled text but also improves system stability and user experience.

EOT;

<span></span>