---
title: "MySQL and UTF-8 the Right Way"
description: "When saving data in languages other than English, the character encoding to use is UTF-8."
date: 2018-03-14
author: "Christofer Sandin"
canonical_url: "https://www.republic.se/en/articles/mysql-and-utf-8-the-right-way"
section: "Articles"
---

# MySQL and UTF-8 the Right Way

**When saving data** in languages other than English, the character encoding to use is UTF-8. The major advantage of this is that it supports thousands of different characters, allowing you to save an å as an å in the database "for real" – which makes working with text significantly easier.

**Unfortunately, it is quite common** to encounter problems when attempting to save UTF-8 encoded text in a MySQL database, even when both the database and table have the correct `encoding` and `collation`. The most likely cause of this is that MySQL's connection character set is set to latin1 by default instead of UTF-8. This leads to issues with Swedish and other characters that are not included in the latin1 set... Here’s a solution to the problem that has worked for us.

## If You Administer the Database Yourself

If you have access to the **my.conf** file, you can add the following two lines under the `[mysqld]` section:

```
collation_server=utf8_unicode_ci
character_set_server=utf8

```

to permanently resolve the issue on that server.

## If You Use a Web Hosting Service

However, if you are using a web hosting service, you need to set the NAMES variable to the correct value. You do this in the code after connecting to the database. Here’s how it looks in PHP:

```
mysql_query("SET NAMES utf8");

```

or use the following line to ensure that the collation is also set to the correct values:

```
mysql_query("SET NAMES 'utf8' COLLATE 'utf8_general_ci'");

```

… and you may even need to set the character set manually if it is not set to UTF-8:

```
mysql_query("SET CHARACTER_SET utf8");

```

If MySQL is configured correctly, the first line above should suffice, but it might not hurt to set all of them just to be on the safe side...

If you want more information about this, you can find it in the [MySQL 5.0 Reference Manual, section 9.1.4](http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html).

## Relaterade artiklar

- ### [A Bit of Basic Database Theory](https://www.republic.se/en/articles/a-bit-of-basic-database-theory)

    Publicerad 2018-03-14

    Development

     ![Development](/content/images/icons/tag-utveckling.png)
- ### [SQL in the Database Management System MySQL](https://www.republic.se/en/articles/sql-in-the-database-management-system-mysql)

    Publicerad 2019-12-08

    Development

     ![Development](/content/images/icons/tag-utveckling.png)
- ### [Primary and Foreign Keys in MySQL](https://www.republic.se/en/articles/primary-and-foreign-keys-in-mysql)

    Publicerad 2020-02-02

    Development

     ![Development](/content/images/icons/tag-utveckling.png)
