I have this simple PHP-script, which searches a mySQL database and outputs the result to the user. I used to use ISO-8859-1 as my charset, but was advised to use UTF-8. But I have trouble going from my former charset to the new one.
To clarify some things, I have:
- Created a database and table encoded in
UTF-8with collationutf8_unicode_ci. - Encoded my PHP-file in
UTF-8. - Set meta charset to
UTF-8. - Set all text
mime-typestoUTF-8throughcreate-mime.assign.plin Lighty (Lighttpd).
Now, the problem arises when I retrieve data from the database with characters like ö, ü etc. If I just do echo "ö"; without retrieving it from the database, it works fine. I guess there must be something wrong with the database then?
I've tried the following, and they've solved my problem:
- Set meta charset to
ISO-8859-1(which, for some strange reason works, but breaks the echo'd "ö"). - Set a
utf8_decode()function around the output. - After
mysql_select_db()declared the followingmysql_set_charset('utf8');.
I know that I've found multiple solutions, but I just don't know why it wont work without them? And is it bad practice to use utf8_decode() on output, or the mysql_set_charset() function?