Domanda

I am using a MySQL database to store some strings which contain German umlauts (äüö). The table "testtable" and the column "text" are utf8_bin collated, and PHPMyAdmin tells me the "MySQL connection" is also utf8_bin.

I then use a PHP script to read the strings and display them:

$sql = "SELECT `text` FROM `testtable` WHERE `id`=$id";

$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, false);
mysql_select_db(MYSQL_DATABASE,$db);

$result = mysql_query($sql,$db);
if (!$result or mysql_errno()) die("Something was wrong with the query: $sql");

$rows = array();
while(($rows[] = mysql_fetch_assoc($result)) || array_pop($rows));

foreach ($rows as $r) {echo $r['text']}

The file itself is encoded in utf-8 according to my editor, and the page is declared as utf-8 in the head:

<meta charset="utf-8">

Yet, the text "This is a testmessage äüöß" in the database is displayed as "This is a testmessage ����". When I write umlauts directly into the HTML of the PHP file, or echo umlauts directly, they are displayed correctly, so I figure the encoding mistake must be somewhere between database and PHP server.

What factors that I've overlooked could be messing up the encoding here, or what could I try to figure out where exactly the problem lies?

È stato utile?

Soluzione

PHPMyAdmin tells you only about its own connection charset and not about connection between database and your application. Try to run query

SET NAMES utf8

right after you made a connection

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top