14

I've always used ISO-8859-1 encoding, but I'm now going over to UTF-8.

Unfortunately I can't get it to work.

My MySQL DB is UTF-8, my PHP document is encoded in UTF-8, I set a UTF-8 charset, but it still doesn't work.

(it is special characters like æ/ø/å that doesn't work)

Hope you guys can help!

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Mikkel
  • 707
  • 4
  • 8
  • 15
  • Are your meta tags set as UTF-8? – Jake N Jan 07 '11 at 08:27
  • here are some goo utf-8 tips and solutions about encoding problems: http://www.utf-8.de/index.php?area=tutorials –  Jan 02 '12 at 23:05
  • hmm what exactly doesn't work about those characters? what you paste above is valid utf8 http://hexutf8.com/?q=c3a62fc3b82fc3a5 – jar Oct 05 '16 at 03:11

7 Answers7

21

Make sure the connection to your database is also using this character set:

$conn = mysql_connect($server, $username, $password);
mysql_set_charset("UTF8", $conn);

According to the documentation of mysql_set_charset at php.net:

Note:
This is the preferred way to change the charset. Using mysql_query() to execute 
SET NAMES .. is not recommended.

See also: http://nl3.php.net/manual/en/function.mysql-set-charset.php

Check the character set of your current connection with:

echo mysql_client_encoding($conn);

See also: http://nl3.php.net/manual/en/function.mysql-client-encoding.php

If you have done these things and add weird characters to your table, you will see it is displayed correct.

Rene Terstegen
  • 7,911
  • 18
  • 52
  • 74
  • 2
    Note that as of PHP 5.5.0, `mysql_set_charset` is deprecated, see http://php.net/manual/en/function.mysql-set-charset.php. For reference, the page states: **Warning This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.** – Krøllebølle Apr 03 '15 at 14:35
  • mysqli_set_charset($con,"utf8"); ([w3school](https://www.w3schools.com/php/func_mysqli_set_charset.asp)) – StinkyCat Mar 22 '17 at 10:45
9

Remember to set connection encoding to utf8 as well.

In ext\mysqli do $mysqli->set_charset("utf8")

In ext\mysql do mysql_set_charset("utf8")

With other db extensions you might have to run query like SET NAMES 'utf8'

Some more details about connection encoding in MySQL

As others point out, making sure your source code is utf-8 encoded also helps. Pay special attention to not having BOM (Byte Order Mark) - it would be sent to browser before any code is executed, so using headers or sessions would become impossible.

Mchl
  • 61,444
  • 9
  • 118
  • 120
2

After connecting to db, run query SET NAMES UTF8

$db = new db(...);
$db->query('set name utf8');

and add this tag to header

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
Edmhs
  • 3,645
  • 27
  • 39
1

I had the same problem but now its resolved. Here is the solution:

1st: update ur table ALTER TABLE tbl_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

2nd: add this in the head section of the HTML code:

Regards Saleha A.Latif

1

Nowadays PDO is the recommended way to use mysql. With that you should use the connection string to set encoding. For example: "mysql:host=$host;dbname=$db;charset=utf8"

kynnysmatto
  • 3,665
  • 23
  • 29
1

Are you having this error? MySql SELECT UNION Illegal mix of collations Error? Just set you entire mysql to utf 8 then

SET character_set_connection = utf8;
Gerard Banasig
  • 1,703
  • 13
  • 20
1

Try this after connecting to mysql:

mysql_query("SET NAMES 'utf8'");

And encode PHP document in UTF-8 without BOM.

Diablo
  • 3,378
  • 1
  • 22
  • 28