I'm having problems storing Swedish characters in my MySQL database. I want to store them in my table called users
with the collation utf8-bin. Even though I'm using utf8, the characters å ä ö
gets stored as å ä ö
and I don't know why. Retrieving the data and echoing it gives me the same output, with the weird characters instead of å ä ö
. Any help is appreciated.

- 1,919
- 5
- 24
- 35
-
2Make sure the database connection uses the correct encoding, and the output encoding of the page must be set to utf8 as well. – knittl Mar 24 '13 at 14:02
-
How do I set these things to utf8? Never encountered this problem before, so don't know where to start. – Simon Carlson Mar 24 '13 at 14:06
-
Check the header of your page (e.g. with the Web Developer toolbar in Firefox) to ensure the character set is UTF-8. Also, there's a meta tag that specifies the character set too - personally I'd set both to make sure. See [here](http://stackoverflow.com/questions/4279282/set-http-header-to-utf-8-php). – halfer Mar 24 '13 at 14:09
-
Currently I'm using this meta tag: ``. Shouldn't this fix the character set? – Simon Carlson Mar 24 '13 at 14:15
4 Answers
Call
mysql_set_charset("utf8");
After connecting and before making any queries.
Your database charset is just for storage, not for transmission between app and database.

- 138,174
- 23
- 272
- 326
-
If you are using PHP, do _not_ use the `mysql_*` interface. It was deprecated long ago and recently removed from PHP. – Rick James Oct 18 '19 at 17:07
There are several places, where you have to pay attention to the encoding.
- Database: you already use an utf8 collation, so that's fine
- Database connection: use
mysqli_set_charset
to set the charset of the connection, if you're using mysqli. Other database drivers have similar functions. - Output encoding of the page: You can use HTTP headers or meta tags. If you want to be on the safe side, specify both.

- 246,190
- 53
- 318
- 364
You should make sure that the database connection uses the Swedish encoding and the encoding of the page output is correct as well. Different encoding causes many of these problems. Read more about character encodings here.

- 8,820
- 20
- 54
- 84
-
How do I do this? I'm using this meta tag: ``, doesn't this cut it? – Simon Carlson Mar 24 '13 at 14:16
There are several parameters that you have to consider here. For this to work well now and in the future. ALL different interactions with the text has to be in same encoding. Even within db (for joins to work well etc).
The encoding of the data beeing inserted (set in header of page and / or utf_8 encoding when inserted).
The encoding in db tables, (i would recommend utf8_swedish for all)
The encoding of the page viewing results from db (set this in header)
The encoding of the page beeing edited. Its possible to open documents in different encoding. This is a big issue if you are not familiar with it. Open and save documents in right encoding.
There use to be a problem concerning the connection encoding to, set this correct, but today it is a smaller problem than a couple of years ago, because of changes.
A couple of notes. Are you sure your data is stored like that, or just presented wrong, via for instance phpmyademin? Try to print with print utf8_encode($text)
Or, utf8_decode() function, that gives you some insight...

- 297
- 1
- 2
- 8