AnonymousJ
AnonymousJ

Reputation: 87

UTF-8 is giving me question marks

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:

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:

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?

Upvotes: 3

Views: 3109

Answers (1)

fullybaked
fullybaked

Reputation: 4127

MySQL is funny with UTF8. You need to ensure the server is running in UTF and that the connection is as well

If you can modify the my.cnf file on your server you can add these to the [mysqld] section and restart it

character-set-server = utf8
skip-character-set-client-handshake

You could alternatively (or as well) use

query("SET NAMES utf8");   

before sending/retrieving data to ensure the database expects UTF8 data to be passed

Upvotes: 1

Related Questions