2

Possible Duplicate:
UTF-8 all the way through

So I saved some data in a mysql database and its stored there with "umlauts". When I'm speaking of umlauts I mean these German letters: ö, ä, ü and their capitalized pendants.

So now I execute a query via PHP, something like this:

$query = "SELECT * FROM tcms_references WHERE id =".$row->id;
$erg = mysql_query($query);
$rowt = mysql_fetch_object($erg);
foreach($row as $x) {
echo $x
}

Now, there is no German word like "Baumfällung" saved in my database. The PHP query is executed and presented on the screen as "Baumfällung".

My question: Where can I set UFT-8 as a Standart in PHP? Is there any function or something?

Community
  • 1
  • 1
Tim Daubenschütz
  • 2,053
  • 6
  • 23
  • 39
  • 1
    Does your MySQL database/table use utf8? – nkr Dec 12 '12 at 22:18
  • 2
    I don't think your problem is with PHP, what is the collation on the database field storing your German word? – Pitchinnate Dec 12 '12 at 22:18
  • Yes, my DB is correctly configured! I tested that via console + query! – Tim Daubenschütz Dec 12 '12 at 22:19
  • 2
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Naftali Dec 12 '12 at 22:20

3 Answers3

5

You need to set mysql connection to utf8

mysql_query ('SET NAMES UTF8;');
mysql_query ('SET COLLATION_CONNECTION=utf8_general_ci;');
mysql_client_encoding($conn);// where $conn is your connection

save page as utf8 and also put this in head of page

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
v0d1ch
  • 2,738
  • 1
  • 22
  • 27
  • Do not use `"SET NAMES"`. Use [`mysql_set_charset`](http://php.net/mysql-set-charset) instead. – nkr Dec 12 '12 at 22:24
  • `mysql_set_charset` is also deprecated : http://php.net/manual/en/function.mysql-set-charset.php – Asqan Feb 18 '17 at 13:58
1

Try to set mysql table to utf8 or else use

 echo utf8_encode($x);
Jordi Kroon
  • 2,607
  • 3
  • 31
  • 55
0

This might not be a PHP issue. You should check that the character collation used in your database matches the one you're using to gather these special character. If those don't match there is no way the DB can save the right value you request.

usumoio
  • 3,500
  • 6
  • 31
  • 57