Json回应给了我不同的输出

时间:2017-02-14 17:40:28

标签: php mysql json

我在json响应中获得了第1行,第2行和第3行的相同值。第二行的假设结果显示在第二列中,第一列中假设的第三个结果显示在第三列。

 $sql = "select
          e_name,
          a_shortcut,
          case
            when t_rank = 1 then '1st'
            when t_rank = 2 then '2nd'
            when t_rank = 3 then '3rd'
          end as t_rank
        from
          team inner join event on team.EID = event.eid Where e_type = 'nonsport' group by event.eid";                         

         $con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name); 

        $result = mysqli_query($con,$sql);


         $response = array();

        while($row=mysqli_fetch_array($result))
        {
        array_push($response, array("e_name"=>$row[0],"1st"=>$row[1], 
        "2nd"=>$row[2], "3rd"=>$row[2]));

        } 
        echo json_encode (array("nresults"=>$response));

如果我的第2行和第3行是[2]和[2],这就是我在服务器响应中的内容。

 {"nresults":[{"e_name":"Amateur Photography Contest","1st":"AAA","2nd":"3rd","3rd":"3rd"}]}

如果我改变了

1st"=>$row[1], 
        "2nd"=>$row[2], "3rd"=>$row[2] into 1st"=>$row[1], 
        "2nd"=>$row[1], "3rd"=>$row[1]

变得像这样

{"nresults":[{"e_name":"Amateur Photography Contest","1st":"AAA","2nd":"AAA","3rd":"AAA"}]}

我的预期输出是:

{"nresults":[{"e_name":"Amateur Photography Contest","1st":"AAA","2nd":"BBB","3rd":"CCC"}]}

其中“AAA”为1级,“BBB”为2,“CCC”为3。

有人能帮助我吗?

0 个答案:

没有答案