user2462246
user2462246

Reputation: 1

parsing json url using php

I am trying to develop the back-end of an android project which is web side. And I don't know how to pass data from android to web. I want to store contents from the android app to my database which is in my server and also want to retrieve some data from database to the android app. I'm using php.

<?php

  $link = mysqli_connect("localhost", "root", "", "location");

  if (mysqli_connect_error()) {
    die("Could not connect to database");
  }

  header('Content-Type: application/json');

  $query1 = "SELECT name,email FROM users WHERE id=2";

  if ($result1 = mysqli_query($link, $query1)) {
    $data = mysqli_fetch_array($result1);

    $json_en = json_encode($data);
    $array = json_decode($json_en, true);

    $name = $array['name'];
    $email = $array['email'];

    echo $name.",".$email;

    $quer = "INSERT INTO users(name,email) VALUES('$name', '$email')";

    mysqli_query($link, $quer);
}

I want to pass this output to android. How to do that?

Upvotes: 0

Views: 32

Answers (1)

Damon Swayn
Damon Swayn

Reputation: 1344

You want to look into developing some kind of backend, whether it respond via JSON, or via XML or any other kind of protocol is up to you.

To store data, you would have the app make a POST request to your PHP endpoint which do handle the storage and would return some kind of status code to the app so that you might show the appropriate messaging. In the case of receiving data, you want to have the app make a GET request with the PHP file loading the data, converting it into your transfer protocol and echo'ing that out as your response.

The code you provided is not properly formatted, but by the looks of it, you are looking to have the result of a query returned to your application. In this case you can just use echo to output the results and it will be returned as the response content to your application.

Upvotes: 1

Related Questions