doudou8081 2016-11-25 07:16
浏览 103
已采纳

俄语字符的json_encode错误

I am having trouble with the output of json_encode. I need to output Russian characters.

In my database table only Russian characters. In the output I am getting only "????????" question marks replaced Russian characters. I read many similar questions but none of them offered a real solution. I tried the following but none of them helped.

Below is my php code.

  1. added ``header ('Content-type: application/json; charset=utf-8');`
  2. used json_encode($albums, JSON_UNESCAPED_UNICODE);
  3. tried 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);
?>
  • 写回答

1条回答 默认 最新

  • douzoudang1511 2016-11-25 07:40
    关注

    You need to set UTF8 before retrieving results from mysql.

    Just before you retrieve results from albums table, fire below query:

    mysqli_query($con, 'SET names UTF8');
    

    After this you can fetch your album results:

    $query = "select * from albums;";
    $result = mysqli_query($con, $query);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?