PHP mysqli_set_charset() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_set_charset() sets the client character set for the connection, for example utf8mb4.

Call it right after you connect and before you escape strings or send text queries. A matching charset keeps multi-byte text correct and makes escaping trustworthy. Prefer utf8mb4 for full Unicode including emoji.

Syntax

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

PHP
$ok = mysqli_set_charset($mysql, 'utf8mb4');
CallWhat it does
mysqli_set_charset($link, 'utf8mb4')Sets the connection charset to utf8mb4.
$mysqli->set_charset('utf8mb4')Object-oriented form.
mysqli_character_set_name($link)Reads back the current charset after a successful set.

Setting the charset with a raw SET NAMES query is easy to get wrong. mysqli_set_charset() is the supported helper. Always set charset before mysqli_real_escape_string() / escape_string().

Library declarations
PHP
mysqli_set_charset(mysqli $mysql, string $charset): bool

Parameters

ParameterDescription
mysqlRequired. Procedural style only: an open mysqli connection.
charsetRequired. Charset name accepted by MySQL, commonly utf8mb4.

Return Value

Return ValueDescription
trueThe charset was set.
falseThe charset could not be set.

PHP Examples

Example 1 — Set utf8mb4 After Connect

Fixed demo that prints the charset you intend to use.

PHP
<?php
$charset = 'utf8mb4';
$ok = true; // stand-in for mysqli_set_charset($link, $charset)
echo $ok ? "charset={$charset}" : 'set_charset failed', PHP_EOL;

How It Works

  1. Connect to MySQL first.
  2. Immediately call mysqli_set_charset($link, 'utf8mb4').
  3. Then run queries or escapes under that charset.
  4. Sample output when connected to a demo database confirms utf8mb4 is active.

Example 2 — Charset Before Escape

Teach the safe order of operations.

PHP
<?php
$order = [
    '1 connect',
    '2 set_charset utf8mb4',
    '3 real_escape_string / prepare',
];
foreach ($order as $line) {
    echo $line, PHP_EOL;
}

How It Works

  1. Wrong charset before escape is a classic footgun.
  2. set_charset() aligns client and connection encoding.
  3. Prepared statements avoid most escaping needs entirely.
  4. Keep this order in every connection bootstrap.

Example 3 — Failed Charset Name

Handle a false return when the name is invalid.

PHP
<?php
$ok = false;
echo $ok ? 'ok' : 'set_charset failed — check the charset name', PHP_EOL;

How It Works

  1. An unknown charset name returns false.
  2. Inspect mysqli_error() for details.
  3. Do not continue as if text were safe to escape.
  4. Fix the configured charset string and retry.

Frequently Asked Questions

It sets the character set used by the MySQLi connection for sending and receiving text.
utf8mb4 is the usual modern choice for full Unicode.
real_escape_string() depends on the connection charset. Setting charset first keeps escaping correct.
Prefer mysqli_set_charset(). It is the dedicated API and avoids incomplete SET NAMES patterns.
Yes. Prepared statements are safer than building SQL with escaped strings.

Next: mysqli_set_opt()

mysqli_set_opt() is an alias of mysqli_options() — it sets a connection option before you .

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