0

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"
        }
    ]
}
8
  • you didn't get the result of your query Commented Jan 17, 2016 at 15:23
  • @DiegoMariani how can i do that. get result and generate response Commented Jan 17, 2016 at 15:25
  • add this: $response["Feeds"] = mysqli_fetch_array($conn, $result); Commented Jan 17, 2016 at 15:25
  • 1
    Remove the $conn then, my bad sorry $response["Feeds"] = mysqli_fetch_array($result); Commented Jan 17, 2016 at 15:30
  • 1
    Look up mysqli and prepared statements. Read about it here: php.net/manual/en/mysqli.prepare.php - If you want to know more about SQL-injections, just Google "php mysql sql inejctions" and you will find a lot of info. Commented Jan 17, 2016 at 15:47

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

And if your PHP-version is compiled with mysqlnd, then you can use: $response['Feed'] = mysqli_fetch_all($result); instead of the while-loop.
1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.