Orofold Labs

Launching soon

Notes Get in touch

Engineering 4 min read Updated

UTF-8 from the editor to the database

In short

Use UTF-8 in every layer, declare it in the HTTP header and the page, name utf8mb4 explicitly in MySQL, and normalise text to NFC.

Garbled text, such as question marks in place of accents, usually means one layer read bytes in a different encoding from the one that wrote them. The fix is rarely in one place. Check each layer from the editor to the database, and do not forget normalisation, the choice people most often miss.

An encoding is the map between bytes and characters

Characters are stored as bytes. A character encoding is the mapping between those bytes and the characters, and without the right mapping the same bytes look like garbage. The W3C introduction to character encodings says that content authors need to declare the encoding of their documents. It also recommends UTF-8 throughout a system, so that data is not converted between encodings on the way.

Declaring an encoding does not change the bytes. If a file was saved in one encoding and the page declares another, the page shows wrong characters until the bytes and the declaration agree.

Declare UTF-8 at every layer

Each layer has its own setting, and the layers must agree:

LayerWhat to setHow to check
Source filesSave as UTF-8, and check the editor’s default for new filesReopen a file with accented letters and confirm they display correctly
HTTP responseA Content-Type header that names UTF-8The response headers in the browser developer tools
HTML documentA meta charset declaration near the start of the headThe declaration fits within the first 1024 bytes
DatabaseThe utf8mb4 character set, named explicitly for the database, the tables and the connectionA test row keeps the characters you typed

The W3C article on declaring encodings in HTML says that the declaration should fit within the first 1024 bytes of the file, ideally right after the opening head tag. An HTTP header takes precedence over a declaration inside the document, so the two must match. Send the header and keep the meta tag as well, because headers and files can fall out of sync.

Working on something similar in your own product? A few honest sentences are enough – we read every enquiry and reply by email. Apply with your idea

Watch the name utf8 in MySQL

In MySQL, the name utf8 is an alias for utf8mb3, an older encoding that stores at most three bytes per character. It cannot store characters that need four bytes in UTF-8, such as many emoji. The MySQL manual calls utf8 a deprecated alias and recommends utf8mb4. Name utf8mb4 explicitly for the database, the tables and the connection, so that the meaning of utf8 cannot change under you.

Normalise text to one form

The same visible text can be stored in more than one way. An accented letter may be a single code point, or a plain letter followed by a combining accent. The W3C String Matching note advises content authors to use Unicode Normalization Form C (NFC) wherever possible, because most legacy data and current software already produce that form.

Avoid the compatibility forms NFKC and NFKD for most web content. The note says they change the meaning, appearance and processing of text. Normalise incoming text to NFC once, when you receive it, so that two strings that look the same also compare as equal.

A practical checklist

  1. Save every source file as UTF-8, and check the editor setting for new files.
  2. Send a Content-Type header that names UTF-8, and put a meta charset declaration near the top of each HTML page.
  3. Create the database, the tables and the connection with utf8mb4 named explicitly.
  4. Normalise incoming text to NFC once, at the boundary, and store that form.
  5. Test with a string that mixes accents, a non-Latin script and an emoji, and read it back from the database and from the page.

Escaping at the point of output is a separate step, covered in escaping output and stopping XSS. Text from users also belongs in prepared statements, as described in preventing SQL injection.

The statements about encodings, declarations and NFC come from the W3C pages and the MySQL manual. Choosing a collation is a separate decision that depends on your data.

Sources and limits

Common questions

Why do my accents turn into strange symbols?
Usually one layer reads bytes in a different encoding from the one that wrote them. Check each layer in turn, from the source file to the database, so that the declarations match the saved bytes.
Is utf8 in MySQL the same as UTF-8?
Not in the full sense. The MySQL manual says utf8 is a deprecated alias for utf8mb3 and recommends utf8mb4. Name utf8mb4 explicitly in the database, the tables and the connection.
What is NFC and do I need it?
NFC is a normalisation form in which accented letters are usually single code points. The W3C advises content authors to use it wherever possible, so normalise incoming text once, before you store or compare it.

Sources

  1. What is character encoding?, W3C Internationalization. Accessed 14 November 2026
  2. Declaring character encodings in HTML, W3C Internationalization. Accessed 14 November 2026
  3. Character Model for the World Wide Web: String Matching, W3C. Accessed 14 November 2026
  4. Unicode Character Sets, MySQL 8.4 Reference Manual (Oracle). Accessed 14 November 2026

Further reading

Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea

All notes