PHP
PDO
charset
SQL
database connection

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

php
1<?php
2$dsn = 'mysql:host=127.0.0.1;dbname=app;charset=utf8mb4';
3$options = [
4    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
5    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
6];
7
8$pdo = new PDO($dsn, 'app_user', 'secret', $options);
9
10$stmt = $pdo->prepare('INSERT INTO messages(body) VALUES (?)');
11$stmt->execute(["Hello 👋"]);

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:

php
1<?php
2$dsn = 'mysql:host=127.0.0.1;dbname=app';
3$options = [
4    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
5    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4',
6];
7
8$pdo = new PDO($dsn, 'app_user', 'secret', $options);

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.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design