尝试将表情符号传递给使用 utf8mb4 编码的数据库时字符串值不正确

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

我有一个包含字符集 utf8mb4 和排序规则 utf8mb4-bin 的数据库。 table 和我插入的列也有这样的属性。我的 PHP 文件以 UTF-8 格式保存(无 BOM)。我用于 PDO 连接的 $dsn 变量如下所示:$dsn = 'mysql:host=127.0.0.1;dbname=vk_comments;port=3306;charset=utf8mb4;connect_timeout=15'。顺便说一下,我在 PHP 7.2。

然而 MySQL 一直说它得到了 Incorrect string value。如您所见,我在各处都设置了正确的字符集。还有什么问题?

附加信息

输入前的示例值: 我从 MySQL 得到的确切错误:SQLSTATE[HY000]: General error: 1366 Incorrect string value: \u0027\xF0\x9F\x98\x8A\u0027 for column \u0027comment_text\u0027 at row 1

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

之后

我必须添加

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

只有这个有效。