Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect string value when trying to pass emoji to the db encoded with utf8mb4

I have a database with charset utf8mb4 and collation utf8mb4-bin. The table and the column to which I'm inserting also have such properties. My PHP file is saved in UTF-8 (without BOM). My $dsn variable used for a PDO connection looks like this: $dsn = 'mysql:host=127.0.0.1;dbname=vk_comments;port=3306;charset=utf8mb4;connect_timeout=15'. I'm on PHP 7.2, by the way.

And yet MySQL keeps saying that it got Incorrect string value. As you can see, I set the proper charset everywhere. What else could be the issue?

ADDITIONAL INFO

Example value before input: 😊 The exact error I'm getting from MySQL: SQLSTATE[HY000]: General error: 1366 Incorrect string value: \u0027\\xF0\\x9F\\x98\\x8A\u0027 for column \u0027comment_text\u0027 at row 1

like image 990
COOLak Avatar asked Sep 01 '25 10:09

COOLak


1 Answers

After $this->dbh = new PDO($dsn, $user, $password);

I had to add

$this->dbh->exec("set names utf8mb4");

Only this one worked.

like image 60
COOLak Avatar answered Sep 02 '25 22:09

COOLak