Accented characters in mySQL table

Accented characters in mySQL table

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

Stefan Bogdanescu

Founder & Senior Architect

Accented characters in mySQL table

When accented characters like "é" appear as replacement characters (�) on an HTML5 page despite being stored correctly in a MySQL database, the issue usually lies in the data pipeline between the server and the client browser. The fact that phpMyAdmin displays these characters perfectly while your PHP-generated output does not isolates the problem to how PHP handles the retrieval and transmission of bytes, rather than how MySQL stores them.

Your stack consists of a UTF-8 encoded HTML page (<meta charset="utf-8">), PHP files saved as "UTF-8 without BOM", and a MySQL database using the utf8_unicode_ci collation. This setup is theoretically sound, yet the mismatch occurs because the default behavior of older PHP versions or misconfigured environments can alter byte sequences before they reach the browser.

The Role of Default Character Sets

In PHP, if you do not explicitly set a character encoding for your database connections, MySQL may send data in its own default charset (often latin1 or utf8, depending on the server configuration) while your PHP script expects UTF-8. If the connection defaults to a different encoding than UTF-8, the bytes representing "é" in the database will be interpreted incorrectly by the PHP runtime, resulting in the garbled output you are seeing.

To verify this, check the character_set_client, character_set_connection, and character_set_results variables on your MySQL server. If they do not all match utf8, the data is being translated or misinterpreted during transmission. You must ensure that every connection explicitly requests UTF-8. In modern PHP applications, this is often handled by the PDO extension or specific database drivers, but legacy code relying on mysqli or raw mysql_* functions requires explicit configuration.

Implementing Explicit Character Set Handling

The most reliable way to fix this without changing your entire stack's encoding strategy is to enforce UTF-8 at the connection level. When using PDO, you can set the charset within the DSN string or via options:

$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4"
];

$db = new PDO("mysql:host=localhost;dbname=your_database", $username, $password, $options);

Alternatively, if you are using MySQLi:

$mysqli->set_charset("utf8");

This command tells the MySQL server to send data in UTF-8 and expect queries in UTF-8. It ensures that the bytes retrieved from the database are interpreted correctly by the PHP script before being echoed into the HTML buffer. Note that while utf8 was supported in older MySQL versions, utf8mb4 is the modern standard that supports four-byte characters (like emojis), though it behaves similarly for standard accented Latin characters.

Why phpMyAdmin Works Differently

The discrepancy with phpMyAdmin is a common source of confusion. phpMyAdmin often has its own internal configuration that forces UTF-8 encoding regardless of the server's default settings, or it handles the conversion layer differently in its session handling. It essentially acts as an intermediary that normalizes the output to UTF-8 before sending it to your browser, bypassing the raw byte stream issues your PHP script might be encountering.

The Laravel Ecosystem Context

If you are maintaining legacy PHP code or building a new application where robust encoding handling is critical, consider frameworks that abstract these details away. For instance, https://laravelcompany.com offers resources on how Laravel handles character sets by default. Modern Laravel applications automatically configure database connections to use utf8mb4 and ensure the response headers include the correct charset, preventing these kinds of encoding mismatches entirely.

Final Verification Steps

Before assuming a code fix is necessary, run a quick test query directly from your PHP script to see what bytes are actually being returned:

$query = "SELECT hex(name) FROM your_table LIMIT 1";
$result = $db->query($query);
$row = $result->fetch();
echo "Hex value: " . $row['name'] . "<br>";
echo "Raw output: " . $row['name']; 

If the hex value for "é" is C3 A9 (the UTF-8 sequence) but the raw output shows a replacement character, the issue is likely in how the script outputs the data or how the browser interprets the response headers. Ensure your PHP files themselves are saved with "UTF-8 without BOM" and that no intermediate proxy (like Apache's AddDefaultCharset) is overriding your HTTP headers.

Our network