get_charset
In this article, we will focus on the mysqli_get_charset() function in PHP, which is used to return the character set for the database connection. We will
The mysqli_get_charset() function returns the character set that PHP is using for a given MySQL connection — for example utf8mb4, latin1, or utf8. Knowing the active charset matters because it controls how text is encoded between PHP and MySQL: the wrong charset turns accented letters, emoji, and non-Latin scripts into garbled "mojibake" and is a classic source of ? characters in stored data.
This article explains what the function returns, how to read every field of the result, and the practical reasons you would call it.
Syntax
mysqli_get_charset(mysqli $mysql): ?objectIn object-oriented style the same function is available as the $mysqli->get_charset() method.
$mysql— a connection returned bymysqli_connect().- Return value — a
stdClassobject describing the connection's character set, ornullif the charset could not be determined.
What the returned object contains
Unlike functions that return a plain string, mysqli_get_charset() returns an object with several fields. The most useful ones are:
| Property | Meaning |
|---|---|
charset | The character set name, e.g. utf8mb4. |
collation | The collation, e.g. utf8mb4_general_ci. |
dir | Directory of the charset (often empty on modern builds). |
min_length | Minimum number of bytes per character. |
max_length | Maximum number of bytes per character (4 for utf8mb4). |
number | Internal MySQL charset id. |
state | Internal charset state flags. |
Basic usage
Call the function on a valid connection and read the charset property:
<?php
$mysqli = mysqli_connect("localhost", "username", "password", "database");
if (mysqli_connect_errno()) {
die("Connection failed: " . mysqli_connect_error());
}
$charset = mysqli_get_charset($mysqli);
printf("Character set: %s\n", $charset->charset); // e.g. utf8mb4
printf("Collation: %s\n", $charset->collation); // e.g. utf8mb4_general_ci
mysqli_close($mysqli);
?>We connect with mysqli_connect(), check for a connection error, then read the charset and collation fields off the returned object.
Object-oriented style
If you prefer the OOP mysqli API, use the get_charset() method:
<?php
$mysqli = new mysqli("localhost", "username", "password", "database");
$charset = $mysqli->get_charset();
echo "Charset: {$charset->charset}\n";
echo "Max bytes: {$charset->max_length}\n"; // 4 for utf8mb4
$mysqli->close();
?>When would I use it?
- Debugging encoding bugs. When stored text shows up as
?or mangled symbols, confirming the connection charset is the first diagnostic step. - Verifying a
set_charset()call took effect. After callingmysqli_set_charset()you can read it back to be sure the change applied. - Branching on byte width. Reading
max_lengthtells you whether the connection can store 4-byte characters such as emoji (utf8mb4) or only up to 3 bytes (utf8).
Get the name only
If you only need the charset name and not the full object, mysqli_character_set_name() returns it directly as a string:
<?php
$mysqli = new mysqli("localhost", "username", "password", "database");
echo $mysqli->character_set_name(); // e.g. utf8mb4
$mysqli->close();
?>Common gotchas
- Don't confuse it with the MySQL
character_set_clientvariable. This function reports the charset PHP is using to talk to the server; it does not change how columns store data — that is fixed by the table/column definition. - Set the charset explicitly. Relying on the server default is fragile across hosts. Call
mysqli_set_charset($mysqli, "utf8mb4")right after connecting, thenmysqli_get_charset()should reportutf8mb4. - Accessing properties on
null. If the connection failed, the function may returnnull; reading->charsetonnullraises a warning. Always check the connection first.
Conclusion
mysqli_get_charset() returns an object describing the character set of a MySQLi connection — its name, collation, and byte limits. Use it to diagnose encoding problems and to confirm that mysqli_set_charset() applied as expected. For just the name, reach for mysqli_character_set_name().