0

after some research I converted the field and table collation to utf8mb4_bin.

If I paste an emoji in the field directly through phpmyadmin, or if I write the query in the command line, it works fine.

The problem is doing it from the website.

Here is my PDO configuration:

$driver_options = [
    PDO::MYSQL_ATTR_INIT_COMMAND    => "SET NAMES utf8",
    PDO::ATTR_ERRMODE               => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE    => PDO::FETCH_OBJ,
    PDO::MYSQL_ATTR_FOUND_ROWS      => true
];

$db = new PDO('mysql:host='.$host.';dbname='.$database.';charset=utf8mb4;collation=utf8mb4_unicode_ci', $username, $password, $driver_options);

Doing it from my website, the emoticons/emojis are saved as ????.

What else do I need to do to be able to save them correctly?

Thank you

EDIT

Solved it. If you use SET NAMES utf8 in the options, it will overwrite the charset=utf8mb4 in the DSN, so in my case it was ultimately utf8 and not utf8mb4

C. Ovidiu
  • 1,104
  • 2
  • 18
  • 38
  • 1
    Check if [this answer](https://stackoverflow.com/a/279279/2453432) can give you some ideas. – M. Eriksson Nov 04 '19 at 16:37
  • 1
    All it takes, is one wrong charset setting in your application - *everything* needs to be the same charset! I have previously written [**an answer about UTF-8 encoding**](https://stackoverflow.com/a/31899827/4535200) that contains a little checklist, that will cover *most* of the charset issues in a PHP/MySQL application. There's also a more in-depth topic, [**UTF-8 All the Way Through**](https://stackoverflow.com/q/279170/4535200). Most likely, you'll find a solution in either one or both of these topics. – Qirel Nov 04 '19 at 16:40
  • @MagnusEriksson Solved and edited my OP, thank you. – C. Ovidiu Nov 04 '19 at 16:45
  • @YourCommonSense Yeah, honestly it was there for I don't even know how long. Is it really useless? It was in my case it seems – C. Ovidiu Nov 05 '19 at 07:22
  • See "question mark" in https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Nov 09 '19 at 02:13

0 Answers0