1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Solve page encoding problem
This guide concerns you if you encounter page encoding problems and abnormal characters that appear on your pages.
Universal encoding
If you encounter page encoding problems, it is important to check that the encoding of your page is well defined in UTF-8 UTF-8 is a universal encoding that supports most of the characters used in the world. Some files may contain characters that are incompatible with UTF-8 encoding. In this case, you need to convert them to UTF-8 using an appropriate text editor.
If the HTML page coding is e.g. in UTF-8 while the database is in Latin-1 accents will not be correctly interpreted by MySQL. If the pages contain special characters (Arabic, Chinese, accents) they can be displayed 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 with SQL code inside, you need to edit them with software that records in UTF-8.
UTF-8 in PHP
To force site headers in UTF-8 using PHP, you can use the function header()
with the parameter Content-Type
Here is an example of a code that allows you to force character encoding into UTF-8:
<?php
header('Content-Type: text/html; charset=utf-8');
?>
UTF-8 via .htaccess
To force character encoding in UTF-8 via the .htaccess file of your site, 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 character encoding in UTF-8 in 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>
Also make sure that all files used on your website, such as CSS-style files and JavaScript scripts, are also encoded in UTF-8. This ensures that all characters on your website will be displayed correctly.
UTF8 in database
To (re)declare the encoding of databases and tables in UTF-8 in phpMyAdmin, you can follow the following steps:
- Connect 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 drop-down button.
- Select a snack option that uses UTF-8 encoding, for example
utf8_general_ci
. - Click the "Execute" button at the bottom of the page to save the changes.
Add 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 server to server):
$connection = mysql_connect($server, $username, $password);
mysql_set_charset('utf8', $connection);
You can also change this default setting on all your hosting by following this documentation.
Once the MySQL connection is established from a PHP script, you can also specify the type of UTF8 float with these commands:
mysql_query("SET NAMES 'utf8';");
mysql_query("SET CHARACTER SET 'utf8';");
Accents / Special characters
When you retrieve a backup from your MySQL database (called "dump") and import it into your own database, it may be that accents (such as e, Ã , ol) appear in the form of strange symbols, such as question marks (e.g. "?" instead of an accent).
This happens because MySQL backups are usually created using a special format called UTF-8
In this format, letters with accents use more space (they are coded on two bytes, a bit like two "units" of data per character). On servers, it works well, but if you work on your personal computer, you may need to adjust some configurations to make the import work properly using UTF-8
The problem of strange characters usually occurs when there is an encoding misalignment between the backup file and the import. This can happen if the file is encoded in a format (e.g. UTF-8
) but that MySQL expects another encoding (e.g. latin1
).
Here are some solutions:
- Convert file : You can convert the backup file to
UTF-8
to another format, calledlatin1
, before you import it into your database. This may prevent accents from being misinterpreted, but it has limitations. If the file contains characters that cannot be represented inlatin1
(such as some special or non-European characters), you may lose these characters when converting. It is therefore a solution to be used with caution, and it depends on the type of data present in your SQL file. - Specify the correct format when importing : If you did not convert the file, you can indicate, when importing, that the file is in format
iso-latin1
(When importing an SQL file, you can explicitly indicate that the file is in ISO-8859-1, also called Latin1). This aligns the encoding of the file with the one expected by MySQL, which usually solves the problem of poorly displayed characters.
If you see question marks instead of accents, this probably means that the backup file is in a format other than UTF-8
, but that your software is trying to import as if it were in UTF-8
To avoid this, under Linux, you can use a command called iconv
to convert the file to UTF-8 before import. This ensures that encoding is consistent with MySQL's expectations.