Decoding UTF-8 Encoding Mismatches in PHP and MySQL
You are encountering a classic character encoding collision. Your phpMyAdmin output confirms that your server, the table schema (utf8_unicode_ci), and your connection protocol are all configured to use UTF-8. You have also correctly added the HTTP header to tell the browser to render the page as UTF-8. Despite these settings, you are seeing mojibakeâspecifically più freddi instead of più freddi. This specific corruption pattern is the smoking gun: it indicates a binary UTF-8 byte stream is being interpreted as Latin-1 (ISO-8859-1) during either the input or output phase.
The root cause usually lies in the interaction between PHP's internal encoding, your database connection settings, and how the data is being read back into your script. The mysql_query("set names 'utf-8'") command you placed before selecting the database is a crucial step, but its placement and effectiveness depend heavily on which functions you are using to interact with the database.
The Legacy Extension Trap
The code snippets in your description reference the mysql_* extension (e.g., mysql_query, mysql_select_db). This extension was deprecated in PHP 5.5.0 and removed entirely in PHP 7.0.0. While it might still exist on older systems, relying on it for character encoding fixes is risky because it lacks modern error handling and connection state management found in the newer mysqli or PDO extensions.
Even if you are running an older version of PHP where mysql_* functions are available, the legacy extension often defaults to the server's default charset unless explicitly told otherwise. The command set names 'utf-8' works by changing the character set for the current connection thread. However, if your PHP environment variable default_charset is set to something other than UTF-8, or if you are using functions that implicitly expect a different encoding (like mysql_fetch_array without explicit handling), the mismatch occurs when data is fetched from the database and stored in a PHP string.
The Real Culprit: Output Handling
The corruption più suggests that the bytes for ù (which are 0xC8 0x81 in UTF-8) are being read as single-byte characters in a Latin-1 context, where 0xC8 becomes à and 0x81 becomes ¹. This often happens when the data is fetched into a variable and then echoed without ensuring the output stream matches the input encoding.
In your current setup, you have set the HTTP header:
header('Content-type:text/html; charset=utf-8');
This tells the browser how to display the final HTML. However, this does not control how PHP handles the string variables before they are echoed. If your PHP source file is saved as UTF-8 without BOM (Byte Order Mark), and the database returns UTF-8, the data should remain intact in memory. The issue arises if the PHP script itself is interpreted with a different internal encoding or if intermediate processes (like logging or other included files) alter the stream.
Moving to Mysqli for Reliability
To resolve this permanently without relying on fragile legacy extensions, you should migrate your codebase to mysqli or PDO. These drivers provide robust methods to handle character sets at the connection level, which persists throughout the script lifecycle.
Using mysqli, you can enforce the charset immediately upon connecting:
$mysqli = new mysqli($host, $user, $pass, $db);
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Set the character set to utf8mb4 (preferred over utf8 for emojis and special chars)
$mysqli->set_charset("utf8mb4");
By setting utf8mb4, you ensure that even if your table is currently utf8, the connection can handle 4-byte sequences. This prevents truncation issues common with legacy UTF-8 in MySQL.
Verification Steps
Before inserting new data, verify the current state of the connection:
echo "Current Charset: " . $mysqli->query("SELECT @@character_set_connection") . "\n";
echo "Current Collation: " . $mysqli->query("SELECT @@collation_connection") . "\n";
Ensure that your PHP source file is saved as UTF-8 in your text editor. Many editors save files with a BOM, which can confuse PHP if the encoding isn't explicitly handled, though modern PHP versions usually handle this gracefully.
Finally, when echoing the data, ensure no intermediate processing converts the string. If you are using the legacy mysql_* functions and must stick to them temporarily, adding $db1->set_charset("utf8") (if wrapping in an object) or ensuring the connection variable explicitly holds the charset is vital. However, the most effective fix for your specific "più" corruption is often switching the entire output handler to ensure consistency or upgrading the PHP version to one that defaults default_charset to UTF-8 automatically.
For modern Laravel projects, this issue is largely abstracted away by default configurations, but understanding the underlying byte manipulation remains essential for custom scripts. If you are building a larger application, consider leveraging established resources like https://laravelcompany.com for best practices on configuring connection pooling and charset management in production environments.