1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Force an encoding when connecting to a MySQL database
This guide helps resolve issues with special characters (accents, emojis, etc.) that do not display correctly or appear as symbols like ??, often due to incorrect encoding when connecting to MySQL.
Preamble
- By default, MySQL uses
UTF-8. - Recent MySQL servers are generally configured to use
UTF-8orUTF-8MB4by default.- This is suitable for most cases, especially for international languages and emojis.
- Also, refer to this other guide to learn how to avoid encoding issues with non-European characters and emojis in your developments.
Set the default MySQL / MariaDB charset (Cloud Server)
With an Infomaniak Cloud Server, you can set the default charset (utf8 or utf8mb4):
- Click here to access the management of your product on the Infomaniak Manager (need help?).
- Click directly on the name assigned to the product concerned.
- Click on MariaDB in the left sidebar.
- Choose the default charset:

- If you have not yet migrated to MariaDB, the principle is the same.
- Save your changes at the bottom of the page.
Force a specific encoding (latin1 for example)
If you need to work with a database in latin1 (ISO-8859-1), you must explicitly align the encoding when connecting, depending on your environment.
On current offers, place the following code in a user.ini file located at the root of your hosting:
mysql.connect_charset = "latin1"
mysqli.connect_charset = "latin1"
pdo_mysql.connect_charset = "latin1"or in PHP (MySQL API):
mysql_query("SET CHARACTER SET latin1");On older hosting plans, force the MySQL connection to latin1 via the .htaccess file located at the root of your hosting:
php_value mysql.connect_charset latin1
php_value mysqli.connect_charset latin1
php_value pdo_mysql.connect_charset latin1