UTF-8 and German characters?

13,275

Solution 1

I was using this code to get title:

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

I just override that to

$title = $_POST['title'];

Solution 2

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.

Share:
13,275
user1814358
Author by

user1814358

Updated on June 18, 2022

Comments

  • user1814358
    user1814358 3 months

    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.

  • Jonast92
    Jonast92 over 9 years
    Using mysql_* is deprecated and mysql_real_escape_string isn't going to keep your database safe. FYI.
  • martinstoeckli
    martinstoeckli over 9 years
    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.
  • deceze
    deceze over 9 years
    @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.
  • Jonast92
    Jonast92 over 9 years
    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.
  • deceze
    deceze over 9 years
    @Jonast92 Say what now? I'd love for you to clarify your point here: stackoverflow.com/q/12703420/476