W3docs

character_set_name

In this article, we will focus on the mysqli_character_set_name() function in PHP, which is used to retrieve the character set name for the current MySQL

The mysqli_character_set_name() function returns the default character set in use for the current MySQLi connection — for example utf8mb4, utf8, or latin1. This page explains what the function returns, its syntax in both styles, when you'd reach for it, and the encoding gotchas it helps you catch.

What the character set controls

The connection character set tells MySQL how to interpret the bytes you send (in INSERT/UPDATE statements and query parameters) and how to encode the bytes it sends back. If the connection charset doesn't match how your strings are actually encoded, you get mojibake — corrupted accented letters, emoji turning into ????, and silent data loss.

mysqli_character_set_name() is the quickest way to inspect that setting at runtime. It's a read-only diagnostic: it never changes anything. To change the charset you use set_charset().

Syntax

The function has an object-oriented form and a procedural form. They are equivalent — pick whichever matches your codebase.

// Object-oriented style
string $mysqli->character_set_name()

// Procedural style
string mysqli_character_set_name(mysqli $mysqli)
  • Parameter (procedural only): $mysqli — a connection returned by mysqli_connect() or new mysqli().
  • Return value: a string holding the current character set name (e.g. "utf8mb4"). It does not return false; on a valid connection there is always a charset.

How to use mysqli_character_set_name()

Call it on an open connection and print the result:

<?php
$mysqli = new mysqli("localhost", "your_username", "your_password", "database");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: " . $mysqli->connect_error;
    exit();
}

echo "Character set: " . $mysqli->character_set_name();

$mysqli->close();
?>

A typical output, before you set anything explicitly, is whatever the server's default is:

Character set: utf8mb4

The same thing in procedural style:

<?php
$mysqli = mysqli_connect("localhost", "your_username", "your_password", "database");
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    exit();
}

echo "Character set: " . mysqli_character_set_name($mysqli);

mysqli_close($mysqli);
?>

Reading the charset after you change it

Because the function reports the live setting, it's handy for confirming that a call to set_charset() actually took effect:

<?php
$mysqli = new mysqli("localhost", "your_username", "your_password", "database");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: " . $mysqli->connect_error;
    exit();
}

echo "Before: " . $mysqli->character_set_name() . "\n";

$mysqli->set_charset("utf8mb4");
echo "After:  " . $mysqli->character_set_name() . "\n";

$mysqli->close();
?>
Before: latin1
After:  utf8mb4

Why you should set the charset, not just read it

mysqli_character_set_name() only reports the charset — it cannot fix one. For full Unicode support (including emoji and many CJK characters) set utf8mb4 right after connecting:

<?php
$mysqli = new mysqli("localhost", "your_username", "your_password", "database");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: " . $mysqli->connect_error;
    exit();
}

$mysqli->set_charset("utf8mb4");
$mysqli->query("INSERT INTO users (name, email) VALUES ('Jörg 🚀', '[email protected]')");

$mysqli->close();
?>

Notes and gotchas:

  • Prefer utf8mb4 over utf8. MySQL's legacy utf8 is a 3-byte alias that cannot store 4-byte characters such as emoji. utf8mb4 is real, full UTF-8.
  • Don't run SET NAMES ... as a query to change the charset. It updates the server-side session but leaves the client library unaware, which breaks real_escape_string(). Always use set_charset() so both stay in sync.
  • The name returned is the connection charset, which is independent of a column's or table's charset defined in the schema.

Conclusion

mysqli_character_set_name() is a small, reliable diagnostic for confirming which character set a MySQL connection is using. Read it to debug encoding problems, then fix them with set_charset("utf8mb4") so your application stores accented text and emoji correctly.

Practice

Practice
What does mysqli_character_set_name() return?
What does mysqli_character_set_name() return?
Was this page helpful?