【发布时间】:2017-04-09 12:49:54
【问题描述】:
json_encode 的输出有问题。我需要输出俄语字符。
在我的数据库表中只有俄语字符。在输出中,我只得到"????????" 问号替换了俄语字符。我阅读了许多类似的问题,但没有一个提供真正的解决方案。我尝试了以下方法,但没有一个有帮助。
下面是我的php代码。
- 添加了``header ('Content-type: application/json; charset=utf-8');`
- 使用
json_encode($albums, JSON_UNESCAPED_UNICODE); - 试过
mb_convert_encoding($str, 'UTF-8', 'auto');json_encode($albums, 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);
?>
【问题讨论】:
-
抱歉你能在编码前显示数据数组(print_r),那我可以试试
-
我在 cmets 中提交了上面所有的代码。我不明白你所说的“array(print_r)”是什么意思
标签: php json unicode utf-8 cyrillic