1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Force an encoding upon connecting to a MySQL database
This guide helps resolve issues related to 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-8
orUTF-8MB4
by default.- This is suitable for most cases, especially for international languages and emojis.
- Also refer to this other guide to learn how to avoid causing encoding issues with non-European characters and emojis in your developments.
Set the default MySQL / MariaDB charset (Cloud Server)
With a Infomaniak Cloud Server, it is possible to 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 relevant product.
- Click on MariaDB in the left sidebar and then choose the default charset:
If you have not yet migrated to MariaDB, the principle is the same:
Force a specific encoding (e.g., latin1
)
If you need to work with a database in latin1
(ISO-8859-1), you must explicitly set the encoding when connecting, depending on your environment.
For 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