4

I am having some problems with the charset on my website. I can't get the UTF-8 charset to work and I get � instead of Å. Yes, I have searched around the web for answers and I've already used the following code:

<!DOCTYPE HTML>
<head>
    <?php header('Content-type: text/html; charset=utf-8'); ?>
    <meta http-equiv="Content-Type" content="text/html" charset=utf-8" />
    <title><?php echo $db_title; ?></title>
</head>
<body>
    Some content with letters æøå
</body>
</html>

The database uses latin1_swedish_ci.

Any idea what it could be?

Gjert
  • 1,069
  • 1
  • 18
  • 48

3 Answers3

2
  • Set the PHP header tag before any output to the browser (as pointed out by JJ Cantillon)

  • While PHP and HTML use the correct UTF-8, it is worth noting that MySQL uses a reduced version of UTF-8 and so when selecting the Collation of the database tables/fields the "utf8_general_ci" (and similar) are not full character sets and so your characters you want to display will not be stored correctly.

What you need to do is to ensure MySQL uses the complete UTF-8 character set by selecting

Collation = 'utf8mb4'

Also, when you are connecting your script to the database you need to specify the same (utf8mb4) character set for PHP/MySQL communication - so set something like (in as far as MySQLi or PDO etc.)

new PDO('mysql:host=localhost;charset=utf8mb4')  

or for MySQLi read up on http://php.net/manual/en/mysqli.set-charset.php

If you store data in the SQL as utf8mb4 but only transfer the data using standard MySQL-utf8 (3bytes rather than 4bytes) then the character sets will be screwed up in transport.

Reading: https://stackoverflow.com/a/16893103/3536236

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
  • Great to know, I haven't created the insert system yet, but this will definitly keep me ahead of future problems :) – Gjert Oct 06 '15 at 12:21
1

You will need to set the header before you output any html.

Try to set the header before your first HTML tag. EG:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<!DOCTYPE HTML>
<head>
    <meta http-equiv="Content-Type" content="text/html" charset=utf-8" />
    <title><?php echo $db_title; ?></title>
</head>
<body>
    Some content with letters æøå
</body>
</html>
  • I solved the problem by changing the charset in the database connection. There must have been a host update, since it hasn't affected the site before. – Gjert Oct 06 '15 at 10:46
  • 1
    Ahh that makes sense. Good to hear you worked it out. – JJ Cantillon Oct 06 '15 at 10:52
0

After a lot of searching I came across the following question. In the database connection I had to specify that I wanted the UTF-8 charset. I did it the following way:

$db = new PDO('mysql:host=localhost;dbname=NAME;charset=utf8', 'USERNAME', 'PASSWORD');

It must have been a server update from the host that caused the problem, since I haven't been affected by the problem before now.

Community
  • 1
  • 1
Gjert
  • 1,069
  • 1
  • 18
  • 48