latin1_swedish_ci is iso-8859-1, you need to set utf8.
Tables can be of a different character set than default database character set, specially if the character set has been changed after you created the created your database.
If you look on the front page of phpMyAdmin, you’ll notice that when you create a new database, it has a setting for “MySQL connection collation:”. Be sure when you create a new database that both that the database collation are set to utf8. It may be as simple as recreating a new database, copying your current one to the new one, then renaming it.
The collation is only used for sorting and comparison of text (that’s why there’s a language term in there). The actual character set may be different. The most common failure is not in the database but rather in the connection between PHP and MySQL. The default charset for the connection is usually ISO-8859-1. You need to change that the first thing you do after connectiong, using either the SQL query
SET NAMES 'utf-8'; or the mysql_set_charset function.
check to make sure that everything is properly UTF-8:
- Make sure that you are serving your HTML as UTF-8:
- header(“Content-Type: text/html; charset=utf-8”);
- Change your PHP default charset to utf-8:
- ini_set(“default_charset”, ‘utf-8’);
- If your database doesn’t ALWAYS talk in utf-8, then you may need to tell it on a per connection basis to ensure it’s in utf-8 mode, in MySQL you do that by issuing:
- charset utf8
- You may need to tell your webserver to always try to talk in UTF8, in Apache this command is:
- AddDefaultCharset UTF-8
- Finally, you need to ALWAYS make sure that you are using PHP functions that are properly UTF-8 complaint. This means always using the mb_* styled ‘multibyte aware’ string functions. It also means when calling functions such as htmlspecialchars(), that you include the appropriate ‘utf-8’ charset parameter at the end to make sure that it doesn’t encode them incorrectly.
If you miss up on any one step through your whole process, the encoding can be mangled and problems arise. Once you get in the ‘groove’ of doing utf-8 though, this all becomes second nature. And of course, PHP6 is supposed to be fully unicode complaint from the getgo, which will make lots of this easier (hopefully)