3

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

COOLak
  • 379
  • 3
  • 15
  • Please show value before input. Not db configurations. And what particular error do you getting back. – JohnnyB1988 Mar 01 '19 at 15:32
  • Please see the edit – COOLak Mar 01 '19 at 15:36
  • Please code where you are inserting data into db. Whole code from input to insert into db. What sql syntax do you use and etc. – JohnnyB1988 Mar 01 '19 at 15:39
  • I'm inserting it like this: `$sql = "INSERT INTO level_1 (comment_id, user_id, user_name, user_photo, url, comment_text, comment_date) VALUES (:comment_id, :user_id, :user_name, :user_photo, :url, :comment_text, :comment_date)"; $pdo->prepare($sql)->execute($data);`. The variable `comment_text` contains that emoji I specified in the question. – COOLak Mar 01 '19 at 15:41
  • 1
    Similar problem here: https://stackoverflow.com/questions/20411440/incorrect-string-value-xf0-x9f-x8e-xb6-xf0-x9f-mysql – JohnnyB1988 Mar 01 '19 at 15:41
  • maybe you need to set `SET = utf8mb4 COLLATE = utf8mb4_general_ci;` – MoxGeek Mar 01 '19 at 15:44
  • Thanks @JohnnyB1988. I hadn't seen that one. I've read that using `set names` was a bad practice, but it's the only thing that helped. Thank you for the link. – COOLak Mar 01 '19 at 15:48

1 Answers1

2

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

I had to add

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

Only this one worked.

COOLak
  • 379
  • 3
  • 15