1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Solve a page encoding problem
This guide is for you if you encounter page encoding issues and unusual characters appearing on your pages.
Universal encoding
If you encounter page encoding issues, it is important to check that the encoding of your page is set to UTF-8. UTF-8 is a universal encoding that supports most of the characters used around the world. It may happen that some files contain characters incompatible with UTF-8 encoding. In this case, you must convert them to UTF-8 using an appropriate text editor.
If the HTML page encoding is, for example, in UTF-8 while the database is in latin-1, the accents will not be interpreted correctly by MySQL. If the pages contain special characters (Arabic, Chinese texts, accents), they may display correctly on your website but not in phpMyAdmin, or vice versa...
Use a text editor capable of saving your files in UTF-8. If you import text files containing SQL code, you must edit them with software that saves in UTF-8.
UTF-8 in PHP
To force the site headers to UTF-8 using PHP, you can use the header()
function with the Content-Type
parameter. Here is an example of code that forces character encoding to UTF-8:
<?php
header('Content-Type: text/html; charset=utf-8');
?>
UTF-8 via .htaccess
To force character encoding to UTF-8 via your site's .htaccess file, for HTML content add:
AddDefaultCharset utf-8
Header set Content-Type "text/html; charset=utf-8"
and for PHP content, add:
php_value default_charset UTF-8
php_value mbstring.internal_encoding UTF-8
UTF-8 in HTML
To specify the character encoding in UTF-8 in the HTML code, you can use the meta tag charset
:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Titre de la page</title>
</head>
<body>
<!-- Contenu de la page -->
</body>
</html>
Make sure that all files used on your website, such as CSS style sheets and JavaScript scripts, are also encoded in UTF-8. This ensures that all characters on your website will be displayed correctly.
UTF-8 in database
To (re)declare the encoding of databases and tables in UTF-8 in phpMyAdmin, you can follow these steps:
- Log in to your phpMyAdmin control panel.
- Click on the "Operations" tab at the top.
- In the "Table Options" section, look for the "Collation" line and click on the dropdown button.
- Select a collation option that uses UTF-8 encoding, for example
utf8_general_ci
. - Click on the "Run" button at the bottom of the page to save the changes.
Add the PHP function mysql_set_charset
to the connection created by mysql_connect to determine the encoding of this connection (if you do not specify it, the default value of this parameter may vary from one server to another):
$connection = mysql_connect($server, $username, $password);
mysql_set_charset('utf8', $connection);
You can also change this default parameter for your entire hosting by following this documentation.
Once the MySQL connection is established from a PHP script, you can also specify the UTF8 charset type with these commands:
mysql_query("SET NAMES 'utf8';");
mysql_query("SET CHARACTER SET 'utf8';");
Accents / Special Characters
When you retrieve a backup of your MySQL database (called a "dump") and import it into your own database, it may happen that accents (like é, à , ô) appear as strange symbols, such as question marks (for example "?" instead of an accent).
This happens because MySQL backups are generally created using a special format called UTF-8
. In this format, letters with accents use more space (they are encoded on two bytes, a bit like two "units" of data per character). On servers, this works well, but if you are working on your personal computer, you may need to adjust some configurations so that the import is done correctly using UTF-8
. The issue of strange characters generally occurs when there is a mismatch in encoding between the backup file and the import. This can happen if the file is encoded in one format (for example UTF-8
) but MySQL expects another encoding (for example latin1
).
Here are some solutions:
- Convert the file: You can convert the backup file from
UTF-8
to another format, calledlatin1
, before importing it into your database. This can prevent accents from being misinterpreted but it has limitations. If the file contains characters that cannot be represented inlatin1
(such as certain special or non-European characters), you risk losing these characters during the conversion. Therefore, this is a solution to be used with caution, and it depends on the type of data present in your SQL file. - Specify the correct format during import: If you haven't converted the file, you can indicate during import that the file is in
iso-latin1
format (when importing an SQL file, you can explicitly specify that the file is in ISO-8859-1, also known as latin1). This aligns the file's encoding with what MySQL expects, which usually resolves the issue of incorrectly displayed characters.
If you see question marks instead of accents, it probably means that the backup file is in a format other than UTF-8
, but your software is trying to import it as if it were in UTF-8
. To avoid this, on Linux, you can use a command called iconv
to convert the file to UTF-8 before importing. This ensures that the encoding is consistent with MySQL's expectations.