1

I wrote a short script which simply inserts Unicode characters into a MySQL database. It looks like this:

mysql_connect('localhost', 'root', '*') or die(mysql_error());
mysql_select_db('test') or die(mysql_error());
mysql_query("INSERT INTO thetable (thefield) VALUES ('äöüß')") or die(mysql_error());

I generated the script using Notepad++ and it's UTF-8 encoded without BOM.

The database and the table have a utf8_general_ci collation. When I look at the data using PhpMyAdmin then the charset seems to be broken. The characters are not displayed correctly:

äöüß

When I receive the data back in my script then the charset seems to be okay. I dumped it with the right header (header('Content-Type: text/html; charset=utf-8')) and everything looks right.

When I insert data into the table using PhpMyAdmin again, then it is displayed correctly inside PhpMyAdmin, but as soon as I dump it from my Demo script, then the charset is broken again.

I have no idea what the reason could be. The database's charset, the HTTP header and the encoding of the script are consistent and I don't doubt that PhpMyAdmin is working correctly. So where else could I look for the problem?

j0k
  • 22,600
  • 28
  • 79
  • 90
YMMD
  • 3,730
  • 2
  • 32
  • 43

1 Answers1

0

Your getting 2 characters for every one in the original, so its reading it as standard ASCII instead of unicode. you probably need to specify the character set the MySQL Connection is using when you connect.

I'm on my cell, but if u post your DB connect code I can show you how when I get to a computer

Edit - see PHP PDO: charset, set names?

Community
  • 1
  • 1
msEmmaMays
  • 1,073
  • 7
  • 7
  • I did not know that I had to define the encoding seperately! This solved it. Thank you so much! – YMMD Sep 01 '12 at 11:42