PHP PDO charset: DSN charset or SET NAMES?
Codemia Knowledge Hub ·
With PDO and MySQL, set the character set in the DSN (charset=utf8mb4) instead of running SET NAMES after connecting. Use utf8mb4 rather than the old three-byte utf8. If client and server disagree about encoding, text arrives garbled, comparisons behave strangely, and non-ASCII characters can be lost or misread.
Set the charset in the DSN
This is the cleanest approach: the connection starts with the expected encoding instead of switching to it afterward with a manual SQL statement.
When to use SET NAMES instead
Running SET NAMES utf8 after connecting can work, but it is a fallback. When PDO and the driver support charset in the DSN, that setting is more direct and less error-prone. You skip an extra command and the connection is correct from the start.
If you must support an older environment where DSN charset handling is unavailable or unreliable, an initialization command is a practical workaround:
Avoid adding SET NAMES everywhere out of habit. If the DSN charset works on your platform, it is clearer and easier to maintain.
utf8 versus utf8mb4
Many older examples still use utf8. In MySQL that name historically means a three-byte subset, not full UTF-8, so four-byte characters such as emoji often fail. For the full Unicode range, use utf8mb4 for the connection, the database, the tables, and the relevant text columns.
Check the whole path, not just the connection
A correct connection charset cannot make up for a schema that still uses the wrong character set. If text is still corrupted after fixing PDO, inspect the other layers:
- Database and table collation.
- Column definitions.
- HTML page encoding.
- JSON serialization and HTTP headers.
- Legacy data already stored with the wrong encoding.
It is common to fix the connection and then find the real issue was an old table still defined with a mismatched collation.
Prepared statements do not fix charset problems. They help with SQL injection and query correctness, but the bytes still travel over the connection in the charset negotiated for that session. That is why connection charset and schema charset both matter, even in fully parameterized code, and why end-to-end consistency is what prevents mojibake.
Related reading
- PHP with MySQL 8.0 error The server requested authentication method unknown to the client
- phpMyAdmin - Error Incorrect format parameter?
- phpmyadmin logs out after 1440 secs
- phpmyadmin.pma_table_uiprefs doesn't exist
- PHP/MySQL insert row then get 'id
- picking a shardkey for mongodb
- pip install mysql-python fails with EnvironmentError mysql_config not found
- Pivot or equivalent in clickhouse
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.