PHP mysqli_get_charset() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_get_charset() returns a character-set object describing the current connection charset, collation and related settings.

The object includes properties such as charset, collation, dir and number. On failure it returns false. The object style is $mysqli->get_charset(). Pair it with mysqli_set_charset() when you need to change the charset.

Syntax

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

PHP
$cs = mysqli_get_charset($mysqli);
CallWhat it does
mysqli_get_charset($mysqli)Returns the current charset object for the connection.
$cs->charsetReads the charset name, such as utf8mb4.
$cs->collationReads the collation name from the same object.

Always set an explicit charset with mysqli_set_charset() after connecting. Then mysqli_get_charset() is a useful check that the connection is using what you expect.

Library declarations
PHP
mysqli_get_charset(mysqli $mysql): object|false

Parameters

ParameterDescription
mysqlRequired. An open mysqli connection.

Return Value

Return ValueDescription
object|falseA charset info object on success, or false on failure.

PHP Examples

Example 1 — Read the Connection Charset

Connect, set utf8mb4, then print the charset name from a fixed sample.

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

echo $cs->charset, PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. mysqli_connect() opens the demo connection.
  2. mysqli_set_charset() selects utf8mb4.
  3. mysqli_get_charset() returns the charset object.
  4. The charset property prints utf8mb4.

Example 2 — Inspect Charset and Collation

Print both charset and collation properties from the returned object.

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

echo $cs->charset, " / ", $cs->collation, PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. The charset is set to utf8mb4.
  2. get_charset() fills the info object.
  3. charset and collation are printed together.
  4. Exact collation names can vary by server; this is a typical sample.

Example 3 — Object Style get_charset()

Use the OO method on the mysqli object after setting the charset.

PHP
<?php
$mysqli = new mysqli("localhost", "demo", "secret", "shop");
$mysqli->set_charset("utf8mb4");
$cs = $mysqli->get_charset();

echo $cs->charset, PHP_EOL;
$mysqli->close();

How It Works

  1. new mysqli() creates the connection object.
  2. set_charset() configures utf8mb4.
  3. get_charset() returns the same style of object.
  4. The sample charset name is printed.

Frequently Asked Questions

It returns an object describing the current connection character set and related settings, or false on failure.
Call mysqli_set_charset($mysqli, "utf8mb4") (or $mysqli->set_charset("utf8mb4")), then optionally confirm with get_charset().
Use $mysqli->get_charset() on a mysqli connection object.
Common properties include charset, collation, dir, min_length, max_length, number, state, and comment.
Setting utf8mb4 on the connection is the important step. get_charset() only reports the current setting. Still use prepared statements for user values.

Next: mysqli_get_client_info()

mysqli_get_client_info() returns a string describing the MySQL client library version that.

mysqli_get_client_info() 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