简体   繁体   中英

Generate JSON Response from PHP

Im new to PHP and. im making mobile app and also building web service for it. from mobile im sending 2 parameters as a POST and want to retrieve data as a JSON data.

below is my PHP code.

header('Content-type: application/json');
include 'connection.php';
$response = array();

$location = $_POST['location'];
$country = $_POST['country'];


        $query = "SELECT * FROM Feeds WHERE location='".$location."' AND country = '".$country."'";
        $result = mysqli_query($conn, $query);


        if (mysqli_num_rows($result) != 0)
      {

          $response["Result"] = 1;
          $response["message"] = "Here Data";

      }else{
          $response["Result"] = 0;
          $response["message"] = "No Data";

      }



echo json_encode($response);
$conn->close();

when i test current response is like below.

{"Result":1,"message":"Here Data"}

but i want to retrieve result data as well along with above response message. like below

{
    "Result": 1,
    "Message": "Here Data",
    "Feeds": [
        {
            "userid": "2",
            "name": "Demo",
            "address": "Demo"
        },
         {
            "userid": "2",
            "name": "Demo",
            "address": "Demo"
        }
    ]
}

You want to itterate over the result from the SQL query too.

header('Content-type: application/json');
include 'connection.php';
$response = array();

$location = $_POST['location'];
$country = $_POST['country'];


    $query = "SELECT * FROM Feeds WHERE location='".$location."' AND country = '".$country."'";
    $result = mysqli_query($conn, $query);


    if (mysqli_num_rows($result) != 0)
  {

      $response["Result"] = 1;
      $response["message"] = "Here Data";
      while($feed = mysqli_fetch_array($result, MYSQLI_ASSOC){
          $response['Feeds'][] = $feed;
      }

  }else{
      $response["Result"] = 0;
      $response["message"] = "No Data";

  }



echo json_encode($response);
$conn->close();

Here the solution. You should also push your query result in your $response array. Used the mysqli_fetch_assoc function. header('Content-Type: application/json') must be called before any actual output is sent, I suggest you to put in the end of your script, to avoid possible mistakes

include 'connection.php';
$response = array();

$location = $_POST['location'];
$country = $_POST['country'];


$query = "SELECT * FROM Feeds WHERE location='".$location."' AND country = '".$country."'";
$result = mysqli_query($conn, $query);


if (mysqli_num_rows($result) != 0) {

    $response["feeds"] = [];

    while ($row = mysqli_fetch_assoc($result)) {
        $response["feeds"][] = $row;
    }

    $response["Result"] = 1;
    $response["message"] = "Here Data";

} else {
    $response["Result"] = 0;
    $response["message"] = "No Data";
}

$conn->close();


header('Content-type: application/json');
echo json_encode($response);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM