0

is there someway to add these characters to a database and have them display properly. I would do something like a find replace, but it appears that my database cannot properly display the β regardless.

When the β is submitted it is changed to "ß" and when "ä" is submitted it is changed to "ö". Thanks for the help.

I am using utf8_unicode_ci collation.

mpn
  • 1,000
  • 1
  • 13
  • 33
  • which collation are you using in your database? – marcosh Feb 04 '14 at 13:48
  • utf8_unicode_ci @marcosh – mpn Feb 04 '14 at 13:49
  • what happens if you try to insert german characters directly from mysql? – marcosh Feb 04 '14 at 13:51
  • when i change this in phpmyadmin it works just fine. @marcosh – mpn Feb 04 '14 at 13:51
  • Collation is for sorting and comparing characters not for data encoding. Save your PHP script as UTF-8 + set the connection to UTF-8. Do not use `SET NAMES` as it brings security risks. Also set the HTTP connection to UTF-8. [Character encoding happens at several stages](http://blog.flowl.info/2014/mastering-unicodeutf-8-encoding-php/)! – Daniel W. Feb 04 '14 at 14:04
  • @DanFromGermany both my php connection and my table in php are UTF8 encoded... and this still doesn't seem to work. – mpn Feb 04 '14 at 14:09
  • @pwneth What do you mean by PHP connection? If it works with phpMyAdmin but not with your script, either your script (the file itself) or the http connection are NOT utf-8 encoded. Or you copied ISO characters into a UTF8 script and did not convert the data. – Daniel W. Feb 04 '14 at 14:13
  • adding array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") to the end of my PDO connection was the only solution that made this work. Thanks for all the help – mpn Feb 04 '14 at 14:33

2 Answers2

1

Make sure your tables are utf8_unicode_ci encoded, and so are the characters that you send them. If you keep everything tidy and utf-8 your β's will go through just fine.

The problem of wrong encoding can occur during any step from the input to the database, so share some more information on that matter and we could help you out further. This will essentially remain your answer though.

Spork
  • 1,631
  • 1
  • 21
  • 37
  • the db is utf8 encoded. and when i add something directly to the DB in phpmyadmin i can use these characters. Perhaps it is an issue with php? is there a way to force the characters to send correctly? – mpn Feb 04 '14 at 13:52
  • It's not an issue with PHP. You need to confirm to us your input is utf-8 encoded, and what functions handle this data before it's given to mysql. Somewhere along the way, something isn't utf8. – Spork Feb 04 '14 at 13:58
  • 1
    Make shure that also the php-files are utf8 encoded. –  Feb 04 '14 at 14:00
  • The table and database collation is not for data encoding. It tells the mysql server which charset to use for sorting and comparing strings. – Daniel W. Feb 04 '14 at 14:07
1

One thing you may be missing is when you setup the connection. There you should also set the encoding to utf8.

Example:

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'test');

/* change character set to utf8 */
mysqli_set_charset($link, "utf8");

or

$pdo = new PDO("mysql:host=localhost;dbname=world;charset=utf8", 'my_user', 'my_pass');

Also make sure your php file is utf-8 encoded. Add following at the start:

header('Content-Type: text/html; charset=utf-8');
Aris
  • 4,643
  • 1
  • 41
  • 38
  • does this work with PDO as well? – mpn Feb 04 '14 at 13:55
  • sure, same idea applies there. see my updated answer. – Aris Feb 04 '14 at 13:59
  • great thanks. tried this, and it seems to screw up the database even worse. Instead of what I was getting before I am now getting: “β†and “ä, ü, ö, ë†for “β” and “ä, ü, ö, ë” – mpn Feb 04 '14 at 14:05
  • maybe something is wrong with the encoding of your php file. see updated answer. – Aris Feb 04 '14 at 14:08
  • I added that to the header and it still did not work. :/ – mpn Feb 04 '14 at 14:13
  • adding "array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")" to the end of my PDO connection solved the problem. thanks. – mpn Feb 04 '14 at 14:34