Storing and displaying unicode string (हिन्दी) using PHP and MySQL

Storing and displaying unicode string (हिन्दी) using PHP and MySQL

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

Stefan Bogdanescu

Founder & Senior Architect

Storing and displaying Unicode string (हिन्दी) using PHP and MySQL

Storing Hindi text in a database and rendering it correctly on a webpage involves three distinct layers of encoding: the database storage, the application logic, and the HTTP response sent to the browser. If you encounter "??????" when trying to display characters like सूर्योदय, the issue is almost always a mismatch between these layers rather than a limitation of UTF-8 itself.

Database Configuration

Your initial setup regarding the database schema was correct in principle but had a specific flaw in the DEFAULT CHARSET. You defined the table column as:

CREATE TABLE `hindi` (
  `data` varchar(1000) character set utf8 collate utf8_bin default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

While the specific column accepts UTF-8, the table's default character set is set to latin1. In older versions of MySQL (pre-5.6), if you insert a multi-byte sequence into a column where the client connection or server context defaults to Latin-1, the database will store the bytes literally but may misinterpret them depending on how they were transmitted.

To ensure the bytes are stored exactly as intended, you must align the connection's character set with the data you are inserting. The critical missing piece in your original attempt was ensuring the MySQL session knows it is receiving UTF-8 bytes before executing the INSERT statement. Using the deprecated mysql_query() function (as seen in your source material), you would execute:

$result = mysql_query("SET NAMES utf8");

The SET NAMES utf8 command performs three essential tasks: it sets the character set for communication between the client and server, matches the connection's character set with the database table's character set, and updates the internal connection state. This ensures that when you copy-paste 'सूर्योदय', MySQL stores the actual UTF-8 bytes (e.g., 0xE0 0xA4...) rather than attempting to map them to a Latin-1 single-byte encoding.

Application Logic vs. Browser Rendering

The confusion often arises because developers conflate the string representation in code with the byte representation sent over the network. In modern PHP, you should never use utf8_encode() on a string that is already UTF-8 encoded. The function utf8_encode() was designed to convert strings from ISO-8859-1 (Latin-1) to UTF-8. If your PHP source file is saved as UTF-8 and your variables hold valid Unicode characters, applying utf8_encode() double-encodes the data, resulting in garbled text or high Unicode code points when echoed.

To display the fetched data correctly, you simply echo the string:

while ($myrow = mysql_fetch_row($result)) {
    echo $myrow[0]; 
}

The browser will render 'सूर्योदय' perfectly provided two conditions are met:

  1. The HTTP response header indicates Content-Type: text/html; charset=utf-8.
  2. The HTML document itself (the <html>, <head>, or body) declares UTF-8 via a meta tag or the <meta charset="utf-8"> element.

If you omit these headers, the browser defaults to its own internal encoding (often Windows-1252 or ISO-8859-1), causing it to misinterpret the UTF-8 byte sequences coming from your PHP script. This is likely why your original attempt resulted in question marks; the bytes were correct, but the browser expected a different character set.

Handling Input and Escaping

You mentioned receiving HTML entities like &#2360;&#2370;... when viewing source. These are decimal numeric character references for the specific Devanagari characters. If you copy these directly into your database, they will be stored as text entities rather than the actual Unicode glyphs. While browsers render them fine, it is best practice to store the raw Unicode glyphs (the actual characters) and only escape them when outputting to HTML contexts to prevent XSS vulnerabilities.

The mapping between a typed character and its entity representation is handled automatically by the browser's encoding layer. You do not need a script to convert 'सूर्योदय' to &#2360;... manually for storage purposes. The conversion happens when you write to an HTML file or form input, but for database storage, raw UTF-8 bytes are preferred.

Modernizing the Approach

The code snippets provided in the source material rely on the mysql_* extension, which has been deprecated and removed in PHP 7.0+. For any new project, you must migrate to PDO or MySQLi. Furthermore, relying on utf8 for character sets is insufficient for full Unicode support (including emojis and rare scripts) because UTF-8 only supports up to code point U+FFFF. You should update your database configuration to use utf8mb4 everywhere:

ALTER TABLE `hindi` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
SET NAMES utf8mb4;

This ensures compatibility with the widest range of Unicode characters while maintaining strict binary sorting (utf8mb4_bin) or standard collation as needed. By aligning the database, the PHP connection, and the HTTP headers, you eliminate the need for manual encoding tricks and ensure consistent rendering of Hindi text across all browsers.

Our network