utf8mb4时将符号插入数据库时流明SQLSTATE错误
Lumen SQLSTATE error when inserting symbol into database when utf8mb4
我是 运行 Lumen 5.8,我有一个 table 和我想添加游戏的数据库,我正在使用它进行迁移:
Schema::create('games', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('slug');
$table->string('name');
$table->softDeletes();
$table->timestamps();
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';
});
如您所见,我设置了字符集和排序规则。
另外我添加到.env
:
DB_CHARSET=utf8mb4
DB_COLLATION=utf8mb4_unicode_ci
当我尝试添加 Pokémon
这样的游戏时
$game = new Game();
$game->slug = "pokemon-red"
$game->name = "Pokémon Red"
$game->save();
我明白了
SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xE3\xA9mon-...' for column 'slug' at row 1 (SQL: insert into `games` (`slug`, `name`, `updated_at`, `created_at`) values (pokmon-white-version-2, Pokémon White Version 2, 2019-04-08 05:28:57, 2019-04-08 05:28:57))
我不确定为什么我仍然收到错误。我可以毫无问题地将它直接插入到 PHPMyAdmin 中的一个字段中,所以我认为它与 Lumen 特别有关。
作为参考,我首先找到了这个,这就是为什么所有内容都设置为 utf8mb4 的原因:
Cannot insert chinese in mysql database through utf8 encoding ( Warning 1336 incorrect string value)
我特别确保进入并在 PHPMyAdmin 中插入一条记录并且它有效,这让我相信这是一个 Lumen 配置问题。
根据评论,我确定名称实际上并没有像我想象的那样被编码。所以添加
$game->name = utf8_encode("Pokémon Red");
问题已解决!
我是 运行 Lumen 5.8,我有一个 table 和我想添加游戏的数据库,我正在使用它进行迁移:
Schema::create('games', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('slug');
$table->string('name');
$table->softDeletes();
$table->timestamps();
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';
});
如您所见,我设置了字符集和排序规则。
另外我添加到.env
:
DB_CHARSET=utf8mb4
DB_COLLATION=utf8mb4_unicode_ci
当我尝试添加 Pokémon
$game = new Game();
$game->slug = "pokemon-red"
$game->name = "Pokémon Red"
$game->save();
我明白了
SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xE3\xA9mon-...' for column 'slug' at row 1 (SQL: insert into `games` (`slug`, `name`, `updated_at`, `created_at`) values (pokmon-white-version-2, Pokémon White Version 2, 2019-04-08 05:28:57, 2019-04-08 05:28:57))
我不确定为什么我仍然收到错误。我可以毫无问题地将它直接插入到 PHPMyAdmin 中的一个字段中,所以我认为它与 Lumen 特别有关。
作为参考,我首先找到了这个,这就是为什么所有内容都设置为 utf8mb4 的原因:
Cannot insert chinese in mysql database through utf8 encoding ( Warning 1336 incorrect string value)
我特别确保进入并在 PHPMyAdmin 中插入一条记录并且它有效,这让我相信这是一个 Lumen 配置问题。
根据评论,我确定名称实际上并没有像我想象的那样被编码。所以添加
$game->name = utf8_encode("Pokémon Red");
问题已解决!