0

I have to store the text that can be in any language. So I set my mysql table to be ucs2_unicode_ci, it is succesuflly holding russian and chineese:

However my php page when fetching it, is delivering it as question marks.

I tried in my php file to set content-type to all kinds of things with no success:

header('Content-Type: text/html;charset=utf-8');
Noitidart
  • 35,443
  • 37
  • 154
  • 323

1 Answers1

1

utf8mb4 would be much better than ucs2. But, assuming you stick with ucs2, you need to establish ucs2 for the connection. See the advice on set_charset or SET NAMES, depending on your client. Or, at least change character_set_results to ucs2 so that it knows to covert from ucs2 to utf8 when retrieving data.

I've seen hundreds of ways the get messed up characters; this is the first time I've seen this technique. (But, almost no one uses ucs2.)

Rick James
  • 135,179
  • 13
  • 127
  • 222
  • Thanks very much for this information! Can you please explain what I would have to do if I used `utf8mb4`. The solution for me was taken from here: http://stackoverflow.com/a/633788/1828637 . What I did was `mysql_set_charset('utf8', $conn)` worked for me when I was using `ucs2_unicode_ci` on my mysql table field. Is this an unusual/quirky fix for this from your experience? – Noitidart Aug 29 '15 at 19:19
  • Don't use the `mysql_*` interface, switch to `mysqli_*`. – Rick James Aug 29 '15 at 22:35
  • utf8, utf8mb4, and ucs2 are 3 different "character sets". The corresponding "collations" for each begin with the character set name. So, `ucs2_unicode_ci` applies to `ucs2`, `utf8mb4_unicode_ci` applies to `utf8mb4`, etc. – Rick James Aug 29 '15 at 22:38
  • 1
    chris85 points to a document about utf8 all the way through. It apples to utf8mb4, changing only the name. Study that, then let's discuss details. – Rick James Aug 29 '15 at 22:40
  • Thanks very much @RickJames Im loaded with some work right now but as soon as I read/research may i post comment here (in like a week or so or whenever?) and we can do SO chat or in comments here please? :) – Noitidart Aug 30 '15 at 01:06
  • 1
    I'll probably be around to comment/chat. I have an [unfinished blog](http://mysql.rjweb.org/doc.php/charcoll) on character set issues. Your issue is almost novel enough for me to add it to the blog. – Rick James Aug 30 '15 at 01:17
  • Loving it hahah "he computer world realizes that there are other people in the world...." – Noitidart Aug 30 '15 at 01:22
  • 1
    I recognize the arrogance and insularism in my fellow-countrymen. And I like to pepper anything I write with at least one chuckle. – Rick James Aug 30 '15 at 01:41
  • Haha plus rep for that recognition if you're countrymen are Americans :P Mine are too :P I was apart of that group when i fell prey to googles advertising :P – Noitidart Aug 30 '15 at 01:54