PHP mysqli_character_set_name() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_character_set_name() returns the current character set name used by the MySQLi connection.

Object-oriented style calls $mysqli->character_set_name(). The value is a string such as utf8mb4. Use it to confirm the connection encoding after connect or set_charset().

Syntax

Call mysqli_character_set_name() with the values you need. Start with this line.

PHP
$charset = mysqli_character_set_name($mysqli);
CallWhat it does
mysqli_character_set_name($mysqli)Returns the active character set name for the connection.
$mysqli->character_set_name()Object-oriented method form.
mysqli_character_set_name($mysqli) === "utf8mb4"Checks that the connection uses utf8mb4.

This reports the client connection character set, not a table collation. Prefer mysqli_set_charset() (or SET NAMES via a controlled API) right after connecting.

Library declarations
PHP
mysqli_character_set_name(mysqli $mysql): string

Parameters

ParameterDescription
mysqlRequired. The mysqli connection object.

Return Value

Return ValueDescription
stringThe character set name, for example utf8mb4 or latin1.

PHP Examples

Example 1 — Print the Connection Character Set

Connect and read the character set name. Sample output when connected to a demo database.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
echo mysqli_character_set_name($mysqli), PHP_EOL;
mysqli_close($mysqli);

How It Works

  1. mysqli_connect() opens the demo connection.
  2. mysqli_character_set_name() reads the active charset.
  3. The sample server is configured for utf8mb4.
  4. Close the connection when finished.

Example 2 — Set Then Read the Charset

Call set_charset, then confirm with character_set_name.

PHP
<?php
$mysqli = new mysqli("localhost", "demo", "secret", "shop");
$mysqli->set_charset("utf8mb4");
echo $mysqli->character_set_name(), PHP_EOL;
$mysqli->close();

How It Works

  1. new mysqli() creates the connection.
  2. set_charset('utf8mb4') sets a modern Unicode charset.
  3. character_set_name() confirms the setting.
  4. Sample output matches utf8mb4.

Example 3 — Guard Against the Wrong Charset

Fail early if the connection is not utf8mb4.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
mysqli_set_charset($mysqli, "utf8mb4");

if (mysqli_character_set_name($mysqli) !== "utf8mb4") {
    echo "Unexpected charset.", PHP_EOL;
} else {
    echo "Charset OK.", PHP_EOL;
}

mysqli_close($mysqli);

How It Works

  1. set_charset() requests utf8mb4.
  2. character_set_name() returns the active name.
  3. A simple comparison guards against misconfiguration.
  4. The demo path prints Charset OK.

Frequently Asked Questions

A string naming the connection character set, such as utf8mb4. It does not return a collation like utf8mb4_unicode_ci.
Call $mysqli->character_set_name() with no arguments. It returns the same string as the procedural function.
Use mysqli_set_charset($mysqli, 'utf8mb4') or $mysqli->set_charset('utf8mb4') right after connecting.
Related but different. character_set_name() returns only the name string. get_charset() returns an object with more details such as collation.
The table, column, connection and PHP output encoding must all agree. Fix the connection with set_charset, and make sure HTML pages declare UTF-8 too.

Next: mysqli_close()

mysqli_close() closes an open MySQLi connection and frees the related client resources.

mysqli_close() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful