2017-02-14 66 views
0

我在我的json響應中獲得了第1行,第2行和第3行的相同值。第2行顯示的假設結果顯示在第2列,第1列假設的第3個結果顯示在第3列。我試過一切,並改變查詢仍然沒有好處。在此先感謝我的sql查詢返回json響應中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]和[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"}]} 

我的預期輸出。其中AAA爲1級,BBB是2,CCC爲3

這樣

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

哪列是 '1' 的查詢? –

+0

擺脫所有非MySQL的東西,看到http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-一個非常簡單的SQL查詢 – Strawberry

+0

@DaniloBustos第1第2和第3應該是a_shortcut,根據他們的排名 – orange

回答

0

嘗試這樣的:

$sql = "select 
      e_name, 
      a_shortcut, 
      GROUP_CONCAT(case when t_rank = 1 then a_shortcut end separator ',') as rank1, 
      GROUP_CONCAT(case when t_rank = 2 then a_shortcut end separator ',') as rank2, 
      GROUP_CONCAT(case when t_rank = 3 then a_shortcut end separator ',') as rank3 
     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)) 
     { 
     //$ar=explode(',',$row['group_con']); 
     array_push($response, array("e_name"=>$row[0],"1st"=>$row['rank1'], 
     "2nd"=>$row['rank2'], "3rd"=>$row['rank3'])); 

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

編輯我的答案,試試這個。 –

+0

{「e_name」:「Yell Competition」,「First」:「1st」,「Second」:「1st」,「Third」:「3rd」}]}爲什麼我得到這個先生?我把它變成1個對象,但是不同的數據 – orange

+0

編輯我的答案再試一次。 –

相關問題