2016-09-02 5 views
1

私はいつも動作していた古いスクリプトを修正しようとしていますが、今は空のレスポンスです。なぜ私は考えていない。私はwhileループが入力されていることを確認しましたが、num_of_rows = 100、まだ私は応答を得ていません。PHPスクリプトの空のレスポンスデータ取得

<?php 

$response = array(); 


$conn=mysqli_connect("localhost", "***", "***","***"); 


// get all gamelists from gamelists table 
$result = mysqli_query($conn,"SELECT * FROM `abcd`"); 


// check for empty result 
if (mysqli_num_rows($result) > 0) { 


$response["gamelist"] = array(); 

while ($row = $result->fetch_array()) { 
    // temp user array 

    $gamelist = array(); 
    $gamelist["id"] = $row["id"]; 
    $gamelist["ques"] = $row["ques"]; 
    $gamelist["odp_a"] = $row["odp_a"]; 
    $gamelist["odp_b"] = $row["odp_b"]; 
    $gamelist["odp_c"] = $row["odp_c"]; 
    $gamelist["odp_d"] = $row["odp_d"]; 
    $gamelist["comment"] = $row["comment"]; 
    $gamelist["correctanswer"] = $row["correctanswer"]; 
    $gamelist["commentfirst"] = $row["commentfirst"]; 





    // push single gamelist into final response array 

    array_push($response["gamelist"], $gamelist); 
} 
// success 
$response["success"] = 1; 

// echoing JSON response 
echo json_encode($response); 
} else { 
// no gamelists found 
    $response["success"] = 0; 
$response["message"] = "No gamelists found"; 

// echo no users JSON 
echo json_encode($response); 
} 
?> 

enter image description here

出力:

1234444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444 
+0

私のget要求に対する応答は空です。 – Alk

+0

私はこのPHPスクリプトに 'GET'リクエストを送ります。レスポンスは「EMPTY」 – Alk

+0

基本的に何もエコーバックされません、それは意味がありますか? – Alk

答えて

1

コードが動作しているようだが、より良いアプローチは次のようになります - あなたはそれが何かに関係だと言っているので

<?php 

$response = array(); 


$conn=mysqli_connect("localhost", "***", "***","***"); 

if($conn){ 
    // get all gamelists from gamelists table 
    $result = mysqli_query($conn,"SELECT * FROM `abcd`"); 

    if($result){ 
     // check for empty result 
     if (mysqli_num_rows($result) > 0) { 
      while ($row = $result->fetch_array()) { 
       $response["gamelist"][] = $row; 
      } 
      // success 
      $response["success"] = 1; 

      // echoing JSON response 
      echo json_encode($response); exit; 
     } else { 
      // no gamelists found 
      $response["success"] = 0; 
      $response["message"] = "No gamelists found"; 

      // echo no users JSON 
      echo json_encode($response);exit; 
     } 
    }else{ 
     echo "db query error".mysqli_error($conn); exit; 
    } 
}else{ 
    echo "db connection error".mysqli_connect_error(); exit; 
} 
?> 

utf-8の問題。

このリンクは、あなたが有用見つけたものです: -

Why would json_encode returns an empty string

関連する問題