views:

739

answers:

5

hi,i moved data from mysql 4 (they were originally set to latin2 encoding) to mysql 5 and set encoding to utf-8. It looks good in phpMyAdmin, utf-8 is ok. But there are question marks instead of some characters on website! website encoding is also set to utf8 so i dont understand where is the problem. PHP and HTML files are also set to utf8. I have no idea ...

+2  A: 

try query

SET NAMES utf8

before any query in your application

valya
not neccessary. Setting db encoding to utf8 and without using `SET NAMES` works fine for my application.
thephpdeveloper
Yep, most common problem. The check your client encoding or set it with the SQL Command valya already posted. It is enough to add it before every block of statements that you send at once.
Mario Mueller
@Mauris, of course, your way is better and faster, but mine is the most simple solution which can be archieved in every language and environment without rtfm :)
valya
Are the question marks inserted by the database or the browser? I always forget that part... ;)
Franz
A: 

You don't have to set your PHP and HTML files to utf-8.

You just have to set your output encoding to UTF-8 and the browser will display appropriately.

In HTML:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

In PHP:

header('Content-Type: text/html; charset=UTF-8');

When you get a string that is UTF-8 from the MySQL table, it will be UTF-8 all the way to browser output unless you convert the encoding. It's the way that the browser inteprets it.

thephpdeveloper
If you store special characters in your PHP scripts, make sure your scripts are UTF-8 encoded or they won't display correctly. Some IDEs do this automatically and it IS a requirement
David Caunt
+1  A: 

Try setting the MySQL connection to UTF-8:

SET NAMES 'utf8'

And send explicit UTF-8 headers, just in case your server has some other default settings:

header('Content-type: text/html; charset=utf-8');
Franz
A: 

I had this problem recently (I hope its the same problem you are having), I tried many ways but at the end what worked was really simple.

Convert your dumped SQL file to UTF-8 format and then import it.

BW: I used Notepad++ for the conversion.

Darkerstar
A: 

Put .htaccess file in your web-site root with content: AddDefaultCharset UTF-8

and

in your dbconfig set after connection to db:

mysql_query("SET NAMES 'utf8'");

Aliaksandr Harbunou