MySQL版本8.0 PHP版本 7.4
MySQL某表中某字段类型为 point ,查询出来后经过以下php代码被传递到前端,但是发生错误
header('Content-Type:application/json; charset=utf-8');
exit(json_encode([
'code' => $message['code'],
'status' => $message['status'],
'message' => $message['message'],
'data' => $message['data'],
], JSON_UNESCAPED_UNICODE));
通过php错误日志排查到:Malformed UTF-8 characters, possibly incorrectly encoded
原因在于json_encode识别到错误的编码,导致发生错误;所以可以通过下面的方式进行解决:
function fixUtf8($data) {
if (is_array($data)) {
foreach ($data as &$value) {
$value = fixUtf8($value);
}
unset($value);
} elseif (is_string($data)) {
$data = mb_convert_encoding($data, 'UTF-8', 'UTF-8');
}
return $data;
}
或者
$data = mb_convert_encoding($data, ‘UTF-8’, ‘UTF-8’);
原创文章,作者:witersen,如若转载,请注明出处:https://www.witersen.com