json_encode 俄语字符错误
json_encode error for russian characters
我在处理 json_encode 的输出时遇到问题。我需要输出俄语字符。
在我的数据库中 table 只有俄语字符。在输出中,我只得到 "????????"
问号替换了俄语字符。我阅读了许多类似的问题,但其中 none 提供了真正的解决方案。我尝试了以下方法,但其中 none 有帮助。
下面是我的 php 代码。
- 添加了 ``header ('Content-type: application/json; charset=utf-8');`
- 用过
json_encode($albums, JSON_UNESCAPED_UNICODE);
- 尝试过
mb_convert_encoding($str, 'UTF-8', 'auto');
json_encode($专辑, JSON_UNESCAPED_UNICODE);
<?php
$host ="localhost";
$user ="misollar_user";
$pass="12345";
$db="misollar_db";
header ('Content-type: application/json; charset=utf-8');
$con = mysqli_connect($host,$user,$pass,$db);
$query = "select * from albums;";
$result = mysqli_query($con, $query);
$albums = array();
while ($row = mysqli_fetch_array($result)){
array_push($albums,array('id'=>$row[0], 'name'=>$row[1], 'songs_count'=>$row[2]));
}
mysqli_close($con);
echo json_encode($albums, JSON_UNESCAPED_UNICODE);
?>
您需要先设置 UTF8
,然后才能从 mysql 检索结果。
就在您从 albums
table 检索结果之前,触发以下查询:
mysqli_query($con, 'SET names UTF8');
之后您可以获取相册结果:
$query = "select * from albums;";
$result = mysqli_query($con, $query);
我在处理 json_encode 的输出时遇到问题。我需要输出俄语字符。
在我的数据库中 table 只有俄语字符。在输出中,我只得到 "????????"
问号替换了俄语字符。我阅读了许多类似的问题,但其中 none 提供了真正的解决方案。我尝试了以下方法,但其中 none 有帮助。
下面是我的 php 代码。
- 添加了 ``header ('Content-type: application/json; charset=utf-8');`
- 用过
json_encode($albums, JSON_UNESCAPED_UNICODE);
- 尝试过
mb_convert_encoding($str, 'UTF-8', 'auto');
json_encode($专辑, JSON_UNESCAPED_UNICODE);
<?php
$host ="localhost";
$user ="misollar_user";
$pass="12345";
$db="misollar_db";
header ('Content-type: application/json; charset=utf-8');
$con = mysqli_connect($host,$user,$pass,$db);
$query = "select * from albums;";
$result = mysqli_query($con, $query);
$albums = array();
while ($row = mysqli_fetch_array($result)){
array_push($albums,array('id'=>$row[0], 'name'=>$row[1], 'songs_count'=>$row[2]));
}
mysqli_close($con);
echo json_encode($albums, JSON_UNESCAPED_UNICODE);
?>
您需要先设置 UTF8
,然后才能从 mysql 检索结果。
就在您从 albums
table 检索结果之前,触发以下查询:
mysqli_query($con, 'SET names UTF8');
之后您可以获取相册结果:
$query = "select * from albums;";
$result = mysqli_query($con, $query);