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, and utf-8 is okay. However there are question marks instead of some characters on website! The website encoding is also set to utf8 so I dont understand where the problem is.

PHP and HTML files are also set to utf8.

I have no idea...

share|improve this question
6  
Oh, a classic! Like the day when the last IE6 instance is deleted, I will celebrate the day when the last PHP script is moved to PHP 6. (In this far, far future I can then tell my grandchildren about the ISO-8859 monster and its sidekick named Codepage.) – Boldewyn Nov 10 '09 at 13:05
   
im sorry but I of course tried SET NAMES 'utf8' ..on database, didnt help. :( – Adriana Nov 10 '09 at 13:08
Well, you have to execute that query every time your script connects to the database before you execute other queries... – Franz Nov 10 '09 at 13:14
2  
Related question with excellent answer on all the things you need to check: stackoverflow.com/questions/279170/utf-8-all-the-way-through – mercator Nov 11 '09 at 22:10
There might be several mistakes, here is a list of fixes for your encoding problem: sebastianviereck.de/en/… – slaver113 Jan 27 at 10:32

8 Answers

try query

SET NAMES utf8

before any query in your application

share|improve this answer
not neccessary. Setting db encoding to utf8 and without using SET NAMES works fine for my application. – mauris Nov 10 '09 at 13:03
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 Nov 10 '09 at 13:04
@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 Nov 10 '09 at 13:10
Are the question marks inserted by the database or the browser? I always forget that part... ;) – Franz Nov 10 '09 at 13:13

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');
share|improve this answer

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'");

share|improve this answer

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.

share|improve this answer
1  
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 Nov 10 '09 at 13:24

Here is a fix. Set the header to header ('Content-type: text/html; charset=utf-8'); Then print your content using utf8_decode($content). You must have the two to make it work.

share|improve this answer

I found that on my server these had no effect:

ini_set('default_charset','utf-8');
mysql_set_charset('utf8');
header('Content-type: text/html; charset=utf-8');

But everything worked perfect once I added:

$mysqli->query("SET NAMES 'utf8'");

Note: I am using encoding utf8_general_ci, but utf8_unicode_ci works identically in my case.

Hope that helps.

share|improve this answer

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.

share|improve this answer

When you show UTF8 characters on a website but tell the browser to interpret them as Latin1 (or Latin2) you see this kind of gibberish: ß

When you show Latin1 (or Latin2) characters on a website, but tell the browser to interpret them as UTF8, you see question marks.

So my guess is that you switched everything to UTF8 (I mean, you told the DB Engine, the web server and the browser you would be using UTF8), but you didn't actually convert the strings to UTF8.

Do what @Darkerstar said. Convert your dump to UTF8 (Notepad++ can do that easily) and import it again.

share|improve this answer
I know this is an old question. I answered for the sake of those that get here with the same problem. – Sebastián Grignoli Jan 8 at 14:53

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.