utf 8 - PHP and MySQLi UTF8

utf 8 - PHP and MySQLi UTF8

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

UTF-8 Encoding Mismatch in MySQLi: The utf8mb4 Reality Check

You are encountering a classic character set encoding issue where your application believes it is speaking Arabic (or another RTL language), but the database is returning data that looks like garbage to your browser. Specifically, the output نتمنشس is the result of UTF-8 bytes being interpreted as Latin-1 or Windows-1252. This visual corruption happens because the client connection and the storage engine are not perfectly aligned in how they handle multi-byte characters.

Your code snippet shows that you successfully set the connection character set to utf8 via $mysqli->set_charset("utf8"). However, there is a fundamental architectural change in MySQL versions 5.5 and later that renders your current configuration insufficient for modern Arabic text. While utf8 supports up to three bytes per character, it cannot store characters like the Arabic letter "Ø«" (Tha) when they require four bytes, or specific combining characters often found in complex scripts. The storage layer is likely using a collation that truncates these characters or corrupts them upon retrieval if the client expects full Unicode support.

The critical fix involves switching your database configuration from utf8 to utf8mb4. This is not merely a preference; it is a requirement for handling emojis, Arabic, and other complex scripts without data loss.

To resolve this, you must update three distinct layers of your environment: the MySQL server variables, the table schema, and the application connection string.

First, ensure your my.cnf (or mysql.ini) configuration file includes the following lines under the [mysqld] section:

[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

Restarting the MySQL service is mandatory after making these changes. Without this server-level setting, even if you set the connection charset in PHP, the underlying storage might still default to utf8 or latin1, causing the mismatch that results in the garbled text نت....

Second, you must alter your existing tables to match the new character set. The ALTER TABLE command is destructive if not careful, but necessary here:

ALTER TABLE Tbl_About CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Running this query ensures that the column About_Title can store the full byte sequence of Arabic characters. If you skip this step, MySQL will silently truncate or corrupt data when it encounters a character outside the utf8 (3-byte) limit.

Third, review your PHP code. You are currently using $mysqli->set_charset("utf8"). While this sets the connection to utf8, it is best practice to explicitly request utf8mb4 if the server supports it. Update your initialization block:

$mysqli = new mysqli("localhost", "root", "", "Amoozeshgah");

if ($mysqli->connect_error) {
    die("Connect failed: " . $mysqli->connect_error);
}

if (!$mysqli->set_charset('utf8mb4')) {
    printf("Error loading character set utf8mb4: %s\n", $mysqli->error);
} else {
    printf("Current character set: %s\n", $mysqli->character_set_name());
}

Note the removal of mysql_set_charset('utf8'). This older procedural function is deprecated and should not be mixed with the object-oriented $mysqli methods. It can lead to state confusion where the internal connection state differs from what the library thinks it is set to.

Finally, ensure your HTML output matches the database encoding. Add <meta charset="UTF-8"> to the head of your document so the browser renders the UTF-8 bytes correctly as Arabic script rather than Latin glyphs.

By aligning the server storage, the table schema, and the PHP connection to utf8mb4, you eliminate the byte-level mismatch that causes the text corruption. This approach ensures that characters like "Ø«" are stored and retrieved exactly as intended.

Our network