0

I am working on a application where I need to store/show data containing many special characters. I have set database collation utf8. I have set collation of table utf8 and character set as utf8_unicode_ci. It is storing all special characters like é, â. But whenever a character , comes it isn't stored as it is. Like whenever there is a word “attributed†it becomes âattributedâ. I am currently using Laravel 5.2 (PHP) .

What I have tried so far I have set following in my code

  iconv_set_encoding('internal_encoding', 'UTF-8');
  mb_internal_encoding('UTF-8');

I have also tried

$value = array_map("utf8_encode", $array);

But this special character isn't getting stored as it is. Will any one let me know what should I do to get this special character saved as it is.

reformed
  • 4,505
  • 11
  • 62
  • 88
Awais Qarni
  • 17,492
  • 24
  • 75
  • 137
  • have you tried using `ISO-8859-1` instead of `UTF-8`? – Option Feb 15 '17 at 12:03
  • Have you read [this](https://bugs.mysql.com/bug.php?id=37724) post? It advices to change the encoding type – Yassin Hajaj Feb 15 '17 at 12:05
  • Is your data going through a URL? How is this data obtained? Have you tried outputting it on screen? – itsols Feb 15 '17 at 12:06
  • @itsols I am getting data from a csv. before sending in database, I have outputed the data in browser and it goes well but not stored as it is in databaase – Awais Qarni Feb 15 '17 at 12:08
  • Does it also not work if you insert it via the MySQL command line? – apokryfos Feb 15 '17 at 12:13
  • MySQL collation `utf8_uni_code` does not exist. You can get a list of available values with `SHOW COLLATION`. I'm pretty sure Laravel defaults to UTF-8 so everything you try in your code will just corrupt data. – Álvaro González Feb 15 '17 at 12:14
  • [UTF-8 All the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – Mark Baker Feb 15 '17 at 12:14
  • @ÁlvaroGonzález Sorry that was written mistakenly. It is actually `utf8_unicode_ci`. I have updated my question as well – Awais Qarni Feb 15 '17 at 12:21
  • BTW, `é` and `“` are **not** correct. That's what you get if you properly store `é` and `“` as UTF-8 and then misprint them as e.g. Windows-1252. What's your Laravel database configuration? You are using a framework, you should be trying to address this at framework level, not patching all around. – Álvaro González Feb 15 '17 at 13:09
  • I think I know your issue. Try this. First create a UTF encoded textfile. Then paste your contents in it. Now try using it with MySQL. This should do the trick. – itsols Feb 15 '17 at 14:48

3 Answers3

0

try setting your collation to "utf8_general_ci" in your mysql

0

Normally its no problem to store the sign to a database field. Check if all your scripts are in a correct coding.

Set your table and all data in that table to utf8_general_ci then try to change your php file to UTF-8.

header('Content-Type: text/html; charset=utf-8');

And if you have incorrect data then use.

utf8_encode("test");

to encode your string to a correct UTF-8 string. If that isn't working i think your data or string your try to convert is not correct.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
0

For being able to use UTF8 characters in your queries you must run a certain query setting this, just like this:

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

But I've seen that you said you're using Laravel. I haven't been working on it, but I guess this's automatically set by the charset parameter written in config, just like in the screenshot of this question: Laravel UTF-8 To Database

Community
  • 1
  • 1
artur99
  • 818
  • 6
  • 18