3

I am trying to displaying VERY Special Characters (like ) inside of my website, and storing it into my database. When I write special characters like those, they get once displayed, but when I reload the page and the server gets the string from the Database, it gets displayed as "??????? ?????? ?????? ??????" I tried to do a lot of things to fix this, but none of them worked. This isn't a visualization problem, because the header is set right:

<meta charset="utf-8">

What I did try to do:

  • Setting the server's default charset to utf8.
  • Setting the database/table/column collapse and charset to utf8mb4_unicode_ci
  • Changing the connect string to:
$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

$db->query("SET character_set_results=utf8mb4");

mb_language('uni'); 

mb_internal_encoding('UTF-8');

$db->query("SET NAMES utf8mb4");

$db->set_charset('utf8mb4');

But nothing of this things worked. As an addition, I tried (Just for debugging) to change a string in the text field of the database to , and I got an error:

Warning: #1300 Invalid utf8 character string: 'F09D91'

Warning: #1366 Incorrect string value: '\xF0\x9D\x91\xBB\xF0\x9D...' for column 'text' at row 1

I tried really every solution I found here and outsite, but nothing worked. Any help would be appreciated.

Community
  • 1
  • 1
Simone Romano
  • 165
  • 3
  • 10
  • 2
    What about the file's encoding itself? – Funk Forty Niner Aug 25 '16 at 11:37
  • What files should I check exactly? – Simone Romano Aug 25 '16 at 11:44
  • All of them. If you don't know how to check a file's encoding, get a code editor and see what it says under "encoding". – Funk Forty Niner Aug 25 '16 at 11:46
  • I use Notepad++ to edit them, so I'm ok. The files' encoding is UTF-8 Without BOM. – Simone Romano Aug 25 '16 at 11:47
  • Consult the following on Stack http://stackoverflow.com/q/279170/ and see if you can use a different encoding method. Plus, try putting all the code following `$db->query("SET character_set_results=utf8mb4");` before that line. Also try a different file encoding. I've had to do that myself before, oddly enough. It could also be the server's default encoding that may be overriding something, somewhere; I've had that problem before also. – Funk Forty Niner Aug 25 '16 at 11:55
  • Addendum to the above. If you're querying somewhere, as in doing a SELECT, UPDATE or INSERT, then it is important that the encoding codes are placed before the query, should that be the case. – Funk Forty Niner Aug 25 '16 at 11:57
  • I love you so much. That did the trick! Thanks man! – Simone Romano Aug 25 '16 at 12:01
  • You're quite welcome and I'm glad to hear it. I'll post my comment as an answer to a certain extent, if you wish to accept it and mark the question as solved. – Funk Forty Niner Aug 25 '16 at 12:03
  • 1
    Of course, I was waiting for it. – Simone Romano Aug 25 '16 at 12:05

1 Answers1

3

As discussed in comments.

In many cases, you need to place all of the code following
$db->query("SET character_set_results=utf8mb4"); before that line.

Also using a different file encoding could also help.

I've had to do that myself before, oddly enough. It could also be the server's default encoding that may be overriding something, somewhere; I've had that problem before also.

Plus, if you're querying somewhere, as in doing a SELECT, UPDATE or INSERT, then it is important that the encoding codes are placed before the query, should that be the case.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Thanks man, this did the trick. U saved me from insomnia and time-wast. – Simone Romano Aug 25 '16 at 12:06
  • @SimoneVenturas You're most welcome Simone and I know "that" feeling all too well, *cheers* – Funk Forty Niner Aug 25 '16 at 12:07
  • $db->query("SET character_set_results=utf8mb4"); before that line. "that line"? which line? – VishalParkash Sep 22 '17 at 16:11
  • I have a laravel project where i need to make changes in database config file and set 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci' also the column where you are storing special character should have collation 'utf8_unicode_ci' or 'utf16_unicode_ci' – akash varlani Feb 27 '18 at 08:15