如何解决laravel中的“参数无法从排序规则 utf8mb3_unicode_ci 转换为 utf8mb4_unicode_ci ”错误

问题描述 投票:0回答:1

我们的 Laravel 应用程序中有一个表,架构如下所示

1   id  bigint(20) unsigned NULL    NULL    NO  NULL    auto_increment      
2   viewed  tinyint(1)  NULL    NULL    NO  1           
3   type    varchar(191)    utf8    utf8_unicode_ci YES NULL            
4   url longtext    utf8    utf8_unicode_ci YES NULL            
5   p_id    varchar(191)    utf8    utf8_unicode_ci NO  NULL            
6   client_id   varchar(191)    utf8    utf8_unicode_ci NO  NULL        clients(id) 
7   created_at  timestamp   NULL    NULL    YES NULL            
8   updated_at  timestamp   NULL    NULL    YES NULL            

我们用登陆页面 URL 记录此表中的视图,如下所示 // 将视图添加到视图日志表

DB::table('client_p_views_log_30days')->insert([
    'client_id'     => $client_id,
    'p_id'  => $p_id,
    'type'          => $type,
    'url'           => $landing_page,
    'created_at'    => now()->toDateTimeString(),
    'updated_at'    => now()->toDateTimeString()
]);

database.php 字符集和排序规则配置

'charset' => env('DB_CHARSET', 'utf8mb4'),
'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'),

升级到 mysql 8.0 后,我们在日志中收到此错误

[2024-02-07 12:07:12] production.ERROR: PoptinAPI->newViewsCount()->Exception {
    "client_id": "3dae67a03936e",
    "p_id": "5486bde1f0d03",
    "Exception": "SQLSTATE[HY000]: General error: 3988 Conversion from collation utf8mb3_unicode_ci into utf8mb4_unicode_ci impossible for parameter (SQL: insert into `client_p_views_log_30days` (`client_id`, `p_id`, `type`, `url`, `created_at`, `updated_at`) values (hfafafhah, agadfsgag, desktop, https://test.com/💉-testfoo/, 2024-02-07 12:07:12, 2024-02-07 12:07:12))"
} []

我们相信这是因为网址部分中的表情符号💉 正如您在我们的表架构中看到的,该列已经是 utf8_unicode_ci,所以我们不确定如何解决这个问题

我们已经尝试过做

'url' => mb_convert_encoding($landing_page, 'UTF-8', 'HTML-ENTITIES'),

但是表情符号变成了

https://test.com/ð-testfoo/

我们不知道是否可以从那里取回它 其他人遇到过这个问题并且知道如何解决该错误吗?任何帮助表示赞赏

php mysql laravel character-encoding
1个回答
0
投票

表情符号💉无法在

CHARACTER SET utf8mb3
中表示。请添加更多到达您所在位置的步骤。

执行

SELECT HEX(URL) FROM ...
,以便我们可以看到存储的内容。由此,我们也许可以建议下一步。

我怀疑任何转换函数(

mb_*
或其他)是否有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.