19

I moved data from MySQL 4 (they were originally set to latin2 encoding) to MySQL 5 and set the encoding to UTF-8. It looks good in phpMyAdmin, and UTF-8 is okay. However, there are question marks instead of some characters on the website! The website encoding is also set to UTF-8, so I don’t understand where the problem is.

PHP and HTML files are also set to UTF-8.

How can I fix this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adriana
  • 8,464
  • 13
  • 36
  • 37
  • 9
    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
  • 1
    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
  • 3
    Related question with excellent answer on all the things you need to check: http://stackoverflow.com/questions/279170/utf-8-all-the-way-through – mercator Nov 11 '09 at 22:10
  • This [post](http://bit.ly/1ma0wIz) explains how to configure and work with UTF-8 in PHP and MySQL. Hope that saves your time. – Dmitry Pavlov Apr 17 '14 at 15:42
  • See "question mark" in https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Jun 13 '20 at 23:45
  • @Sebastian Viereck: The link is broken (*"Die Seite konnte nicht gefunden werden"*) – Peter Mortensen Feb 21 '22 at 19:53

11 Answers11

30

Try the query

SET NAMES utf8

before any query in your application.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Valentin Golev
  • 9,965
  • 10
  • 60
  • 84
  • 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 :) – Valentin Golev Nov 10 '09 at 13:10
  • 1
    Are the question marks inserted by the database or the browser? I always forget that part... ;) – Franz Nov 10 '09 at 13:13
  • This worked for me, but it was not clear exactly how this was done. Therefore I added a new answer with *exact* PHP syntax below. – Jonathan Cross Nov 14 '15 at 18:24
25

On my server, adding these to my PHP file had no effect:

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

But everything worked perfectly once I added this to the top of my PHP file:

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

Note: I am using encoding utf8_general_ci in my database, but utf8_unicode_ci works the same for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jonathan Cross
  • 675
  • 8
  • 17
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');
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Franz
  • 11,353
  • 8
  • 48
  • 70
2

When you show UTF-8 characters on a website, but tell the browser to interpret them as Latin-1 (or Latin-2) you see this kind of gibberish: ß

When you show Latin-1 (or Latin-2) characters on a website, but tell the browser to interpret them as UTF-8, you see question marks.

So my guess is that you switched everything to UTF-8 (I mean, you told the database engine, the web server and the browser you would be using UTF-8), but you didn't actually convert the strings to UTF-8.

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sebastián Grignoli
  • 32,444
  • 17
  • 71
  • 86
2

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 interprets it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mauris
  • 42,982
  • 15
  • 99
  • 131
  • 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 Snabel-Caunt Nov 10 '09 at 13:24
2
mysql_query("SET NAMES UTF8");

Adding this line at the end of my "connection.php" solved my problem.

My connection file's complete code is:

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_test = "localhost";
$database_test = "test";
$username_test = "username";
$password_test = "password";
$test = mysql_pconnect($hostname_test, $username_test, $password_test) or trigger_error(mysql_error(), E_USER_ERROR);
mysql_query("SET NAMES UTF8");
?>

My database collation is "utf8_general_ci".

Pages are "dreamweaver default utf8" and "unicode normalisation form=C (Canonical Decomposition)".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fahri Akar
  • 21
  • 2
1

I had this problem recently (I hope it’s the same problem you are having), and 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.

BTW: I used Notepad++ for the conversion.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Darkerstar
  • 924
  • 1
  • 8
  • 17
1

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

and

in your dbconfig set after connection to the database:

mysql_query("SET NAMES 'utf8'");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

It doesn't seem like setting every SQL database, table, and field to UTF-8 in MySQL is good enough. Very annoying.

I ended up forcing the issue to solve encoding problems:

I had to use this every place I open the database:

$db->set_charset("utf8");

And that worked. Finally.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3314053
  • 239
  • 1
  • 3
  • 11
0

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.

animuson
  • 53,861
  • 28
  • 137
  • 147
-1

Putting:

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

Where $conn is my Mysqli connection in my config file helped.

Doctert
  • 11
  • 2