我正在处理一个使用 UTF8 编码并且有许多包含特殊字符的用户名的数据库,例如“Ғђ▫ Sony”
查询用户表时,Lumen 以不正确的数据响应。我尝试使用mysqli
and查询同一个表PDO
,我收到了预期的结果。我设置了一个示例路线来测试它:
$app->get("charset", function() {
$mysqli = new mysqli("localhost", "user", "password", "database");
$res = $mysqli->query("select name from users where id = 1");
$dbh = new PDO('mysql:host=localhost;dbname=database', "user", "password");
$stmt = $dbh->query("select name from users where id = 1");
$lumen = DB::select("select name from users where id = 1");
return response()->json([
"mysqli" => $res->fetch_assoc(),
"pdo" => $stmt->fetchAll(PDO::FETCH_ASSOC),
"framework" => $lumen
]);
});
访问路由时,我得到以下响应:
{
"mysqli": {
"name": "Ғђ ▫ Sony"
},
"pdo": [
{
"name": "Ғђ ▫ Sony"
}
],
"framework": [
{
"name": "Ò’Ñ’ â–« Sony"
}
]
}
如果上面的文本无法正确显示,这是响应的屏幕截图:
据我所知,Lumen 的 MySQL 配置默认为 UTF8 并且不可更改 - 我在以下位置找到了以下内容vendor/laravel/lumen-framework/config/database
:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => env('DB_PREFIX', ''),
'timezone' => env('DB_TIMEZONE','+00:00'),
'strict' => false,
],
我不知道是什么原因造成的。我还能做些什么来尝试追踪这种差异?