8

I have problem with German characters on my web site,

in html/php part of website i have this code to set utf-8:

<meta charset="utf-8">

in mysql, i have this code to set utf-8

SET CHARSET 'utf8';
  1. Here is some word on German: Gemäß

  2. Here is how that word looks in mysql table:

    Gem&Atilde;&curren;&Atilde;Ÿ

  3. Here is how that word is shown on the site: Gemäß

What is a problem? Thanks.

Pete
  • 57,112
  • 28
  • 117
  • 166
user1814358
  • 619
  • 3
  • 8
  • 17
  • 1
    How did you put that into the database? Setting the charset doesn't help if you just throw latin-1 bytes in after claiming you're sending utf-8. – Wooble May 22 '13 at 12:06
  • What @Wooble said. You have to make sure the strings you're inserting are in the same encoding. Just because the site's meta tag says UTF-8 doesn't mean the strings in your code will be UTF-8 as well (though I always assumed UTF-8 was the default encoding... I'm seeing I might be doing something wrong here and there too). – Geeky Guy May 22 '13 at 12:08
  • @Wooble In my connection.php is this code: mysql_set_charset('utf8'); I insert data like this: mysql_query("INSERT INTO slike(title,string,material,price,dimensions,lang,currency) VALUES('$title','$code','$material','$price','$dimensions','$lang','$price')") or die(mysql_error()); – user1814358 May 22 '13 at 12:19
  • 1
    possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – deceze May 22 '13 at 12:39
  • you still have wrong encoding in the database, and you should fix it anyways. – Aris May 22 '13 at 13:51

9 Answers9

2

I was using this code to get title:

$title = mysql_real_escape_string(htmlentities($_POST['title']));

I just override that to

$title = $_POST['title'];
AstroCB
  • 12,337
  • 20
  • 57
  • 73
user1814358
  • 619
  • 3
  • 8
  • 17
  • Using mysql_* is deprecated and mysql_real_escape_string isn't going to keep your database safe. FYI. – Jonast92 May 22 '13 at 13:26
  • 2
    Now you have indeed an error, while the first statement only scrambled your output, you now made it dangerous. Always escape your data for the appropriate output and only for this output. To display data on an HTML page you should use `htmlspecialchars()`, to insert a string in an SQL statement use `mysqli_real_escape_string()` or parametrized queries. – martinstoeckli May 22 '13 at 13:26
  • @Jonast92 `mysql_real_escape_string` is perfectly adequate to prevent SQL injection *if applied properly*. The problem is that many people don't apply it properly. – deceze May 22 '13 at 13:37
  • Sure it could be helpful but applying it ONLY won't do much, for example it won't safe you from logical sql injections. You might as-well just skip it, but it doesn't hurt to apply it if all other safeguards are used as-well. – Jonast92 May 22 '13 at 13:49
  • @Jonast92 Say what now? I'd love for you to clarify your point here: http://stackoverflow.com/q/12703420/476 – deceze May 22 '13 at 14:07
1

At first, make sure, that you have UTF-8 characters in your database.

After that, try using SET NAMES 'UTF8' after connecting to MySQL:

$con=mysqli_connect("host", "user", "pw", "db");   
if (!$con)
{
    die('Failed to connect to mySQL: ' .mysqli_connect_errno());
}

mysqli_query($con, "SET NAMES 'UTF8'") or die("ERROR: ". mysqli_error($con));

As the manual says:

SET NAMES indicates what character set the client will use to send SQL statements to the server... It also specifies the character set that the server should use for sending results back to the client.

user4035
  • 22,508
  • 11
  • 59
  • 94
0

Try SET NAMES 'utf8' or SET NAMES 'utf-8'. Some of these works fine for portuguese, probably for german too. I just can't remember which one is correct, but if it is not, an error will be produced.

Henrique Barcelos
  • 7,670
  • 1
  • 41
  • 66
0

you should make sure that the CONNECTION is also utf-8.

with mysqli this is done with something like this:

$connection = mysqli_connect($host, $user, $pass, $db_name);
$connection->set_charset("utf8");

Now if somehow you ended up with wrong characters in the database there is a way to make it right:

  1. in a PHP script, retrieve the information as you do now, i.e without setting the connection. This way the mistake will be inverted and corrected and in your php file you will have the characters in the correct utf-8 format.
  2. in a PHP script, write back the information with setting the connection to utf-8
  3. at this point you should see the character correct in your database
  4. now change all your read/write functions of your site to use the utf-8 from now on
Aris
  • 4,643
  • 1
  • 41
  • 38
0

in HTML5 use

<meta charset="utf-8">

in HTML 4.0.1 use

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Amir
  • 4,089
  • 4
  • 16
  • 28
0

I had the same problem. which I solved by using:

if you have already created your table, you need the modify the character set as:

alter table <table name> convert to character set utf8 collate utf8_general_ci.

your tables character set is set to latin_swedish by default by MySQL.

also, you might face some problems while retrieving the data and displaying it to you page.For that include: mysql_set_charset('utf8') just below the line where you have connected your database. eg:

mysql_connect('localhost','root','');
mysql_select_db('my db');
mysql_set_charset('utf8');
Ferrmolina
  • 2,737
  • 2
  • 30
  • 46
radiopassive
  • 556
  • 1
  • 5
  • 15
0

the results are html entity encoded as if they were processed by htmlentities(), I wonder if your variables are ibserted as received from the form or are being processed by say a wysiwg editor for instance?

Anyway, these should print fine on an html template but an html_entity_decode() should do it to.

Hope this helps

ghousseyn
  • 102
  • 4
0

You will need to do this for php 5.x

$yourNiceLookingString = 
htmlspecialchars ($YourStringFromDB, ENT_COMPAT | ENT_HTML401, 'ISO-8859-1');

and for php 4.x

  $yourNiceLookingString = htmlspecialchars($YourStringFromDB);
Sachu
  • 7,555
  • 7
  • 55
  • 94
xelilof
  • 456
  • 3
  • 11
0

Set the data type in your database to use UTF-8 as well, this should solve the problem.

user3490122
  • 29
  • 1
  • 6