How to save Swedish Characters in MySQL using PHP and display it on Web Page
It was a headache and wastage of time when I came across the situation when I saw strange Swedish characters on one of my clients web page and its MySQL database. After googling I found several suggestions but no one was clearer unless I experience my self and rectified the problem.
Basically there are three places that need to be handled to display Swedish characters correcly on a site that uses PHP and MySQL.
1. HTML Side 2. PHP Side 3. MySQL table structure
Lets talk about each of the above one by one.
1. HTML Side
You have to make sure that html encoding is set to UTF-8. In other words simply add the following to the head section of your html document. Please also make sure to save the webpage in utf-8 format by using notepad++ for example. To do it in notepad++ open the webpage in notepad++ and click menu Encoding-> Convert to utf-8 ( to troubleshoot just create a new document in notepad++ and copy your webpage to it and now click Encoding-> Convert to utf-8).
<meta http-equiv=”content-type” content=”text/html; charset=utf-8″ />
2. PHP Side
To save data to MySQL use PHP function utf8_encode. This mean if you want to save some data to MySQL say ‘åöä’ , you should assign it to a variable like this:
$tmp = utf8_encode(‘åöä’);
and now you can use $tmp in your update or insert MySQL Query.
3. MySQL table structure
The last step that you must do is to goto the table where you are supposed to save data that contains Swedish characters. Click structure of that table (in phpmyadmin) and click ‘change’ in front of the field that will save data with Swedish characters. Simple change the collation to utf8_swedish_ci
You are done.