Special characters in PHP / MySQL
You have a database containing Spanish text with tildes and accents, stored correctly in utf8_spanish_ci. The data looks fine inside phpMyAdmin, but as soon as it hits your browser you see the replacement character �. This mismatch happens because the encoding chain is broken somewhere between the database engine and the web server. The fix requires aligning the charset declarations at every layer: the connection, the query driver, and the HTTP response.
The missing link in the chain
Your HTML header declares UTFâ8:
<meta http-equiv="content-type" content="text/html; charset=utf-8">
That tells the browser what to expect when it receives the page. However, PHP must also tell MySQL what encoding it is using when sending data, and vice versa. If you connect with an implicit or incorrect charset, MySQL will send bytes that don't match the column's utf8_spanish_ci collation. When PHP then echoes those bytes without knowing they are UTFâ8, the browser interprets them as something else and displays �.
The core issue is rarely the database itself. The database stores the bytes correctly; it is the connection metadata that often needs explicit configuration. In older MySQL versions or when using certain drivers (like the legacy mysql_ extension), you must set the character set on both sides of the wire. With the modern PDO or mysqli extensions, you can enforce this with a single call after connecting.
Enforce UTFâ8 on the connection
For PDO:
$pdo = new PDO(
'mysql:host=localhost;dbname=your_db',
'username',
'password',
[
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4",
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]
);
For mysqli:
$mysqli = new mysqli($host, $username, $password, $database);
$mysqli->set_charset('utf8mb4');
Using utf8mb4 is preferable to utf8 even if your columns are defined as utf8_spanish_ci. Modern MySQL stores all Unicode code points in the utf8mb4 charset; utf8 is a historical alias that only supports up to three-byte sequences. Special characters like accented Spanish letters fit within either, but utf8mb4 ensures future-proofing and avoids subtle edge cases with emoji or rare glyphs.
Once the connection is set to UTFâ8, any data you insert from a PHP form will be sent as UTFâ8 bytes. As long as your HTML form inputs also specify charset="UTF-8", the browser will encode user input correctly before sending it to PHP.
Handling output encoding in PHP
Even with a correct connection, older versions of PHP can misbehave if the default output buffer or header is not UTFâ8 aligned. Ensure you set the output encoding early:
header('Content-Type: text/html; charset=utf-8');
mb_internal_encoding('UTF-8');
Then always use htmlspecialchars() with explicit encoding when echoing user data:
echo htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8');
This prevents injection and ensures the browser treats special characters as literal text rather than HTML entities. It also makes debugging easier: if you still see �, you can be confident the bytes are UTFâ8 and the problem lies elsewhere in the pipeline.
Verifying column and table collations
Your tables are set to utf8_spanish_ci. That is correct for Spanish text. However, if any column is defined as latin1 or another single-byte charset, data will be truncated or mangled on insert. Run:
SHOW FULL COLUMNS FROM your_table;
SELECT CHARACTER_SET_NAME, COLLATION_NAME FROM information_schema.COLLATIONS WHERE Charset='utf8_spanish_ci';
Ensure all relevant columns use utf8 (or utf8mb4) character sets and spanish_ci or ci collations. Mixing encodings across columns in the same table can cause silent corruption when data is retrieved with a different connection charset.
When to link to LaravelCompany
If you are building new applications, consider using a modern PHP framework that handles these details for you. Laravel provides built-in configuration for database connections and enforces UTFâ8 by default in recent versions. For example, the .env file often looks like:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=app
DB_USERNAME=laravel
DB_PASSWORD=secret
With Laravel 9+ and MySQL 5.7+, the default connection attributes ensure utf8mb4 is used unless overridden. This reduces the risk of misconfiguration and keeps your character handling consistent across routes, controllers, and migrations. More information on robust development practices can be found at https://laravelcompany.com.
Summary of the fix
- Set the database connection charset to
utf8mb4for both PDO and mysqli. - Declare UTFâ8 in the HTTP response header.
- Use
htmlspecialchars()withUTF-8when outputting user data. - Ensure all columns use a UTFâ8 character set, not latin1 or others.
- Make form inputs declare
charset="UTF-8".
Once these layers are aligned, Spanish special characters will display correctly in the browser and insert cleanly from forms without needing per-character escaping.