1

I have created Database with Columns "id,name,username,email,age,password". And with the php file as below with android activity give below. i always end up with returning with the null values. As for, my android activity doesnt give aways any errors but i find problem with the php file only.

My php file:

<?php 
define('HOST','mysql.hostinger.in');
define('USER','u115908902_willi');
define('PASS','123');
define('DB','u115908902_prac');

$con = mysqli_connect(HOST,USER,PASS,DB)

if($_SERVER['REQUEST_METHOD']=='GET'){

    $id  = $_GET['id'];



    $sql = "SELECT * FROM user WHERE id='".$id."'";

    $r = mysqli_query($con,$sql);

    $res = mysqli_fetch_array($r);

    $result = array();

    array_push($result,array(
        "name"=>$res['name'],
        "username"=>$res['username'],
        "email"=>$res['email'],
                    "age"=>$res['age'],
                    "password"=>$res['password'],
        )
    );

    echo json_encode(array("result"=>$result));

    mysqli_close($con);

   }

Then my MainActivity code is:

  import android.app.ProgressDialog;
  import android.os.Bundle; 
  import android.support.v7.app.AppCompatActivity;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  import android.widget.Toast;

  import com.android.volley.RequestQueue;
  import com.android.volley.Response;
  import com.android.volley.VolleyError;
  import com.android.volley.toolbox.StringRequest;
  import com.android.volley.toolbox.Volley;

  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;

  public class SortId extends AppCompatActivity implements    View.OnClickListener {


private EditText editTextId;
private TextView textViewResult;

private ProgressDialog loading;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sort_id);

    editTextId = (EditText) findViewById(R.id.editTextId);
    Button buttonGet = (Button) findViewById(R.id.buttonGet);
    textViewResult = (TextView) findViewById(R.id.textViewResult);

    assert buttonGet != null;
    buttonGet.setOnClickListener(this);
}

private void getData() {
    String id = editTextId.getText().toString().trim();
    if (id.equals("")) {
        Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
        return;
    }
    loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);

    String url = Config.DATA_URL+editTextId.getText().toString().trim();

    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            loading.dismiss();
            showJSON(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(SortId.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private void showJSON(String response){
    String name="";
    String username="";
    String email="";
    String age="";
    String password="";
    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
        JSONObject collegeData = result.getJSONObject(0);
        name = collegeData.getString(Config.KEY_NAME);
        username = collegeData.getString(Config.KEY_USERNAME);
        email = collegeData.getString(Config.KEY_EMAIL);
        age = collegeData.getString(Config.KEY_AGE);
        password = collegeData.getString(Config.KEY_PASSWORD);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    textViewResult.setText("name:\t" +name+ "\n username:\t" +username+"\n email:\t" +email+ "\n age:\t" +age+ "\n password:\t" +password);
}

@Override
public void onClick(View v) {
    getData();
}
}
William Willi
  • 194
  • 1
  • 2
  • 19
  • Your code is vulnerable to SQL Injection. More info: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Allan Pereira Jun 24 '16 at 12:44

2 Answers2

0

Your php code having some problems, please use below code. You created two arrays i think, Follow the code below

     <?php 
 define('HOST','mysql.hostinger.in');
 define('USER','u115908902_willi');
 define('PASS','123');
  define('DB','u115908902_prac');

 $con = mysqli_connect(HOST,USER,PASS,DB)

 if($_SERVER['REQUEST_METHOD']=='GET'){

$id  = $_GET['id'];



$sql = "SELECT * FROM user WHERE id='.$id';

$r = mysqli_query($con,$sql);

$res = mysqli_fetch_array($r);

$result = array();

array_push($result,array(
    "name"=>$res['name'],
    "username"=>$res['username'],
    "email"=>$res['email'],
                "age"=>$res['age'],
                "password"=>$res['password'],
    )
);

echo json_encode("result"=>$result);

mysqli_close($con);

 }
Harish Reddy
  • 922
  • 10
  • 20
0

Try the following (based on http://php.net/manual/en/mysqli.query.php):

<?php 
define('HOST','mysql.hostinger.in');
define('USER','u115908902_willi');
define('PASS','123');
define('DB','u115908902_prac');

$con = mysqli_connect(HOST,USER,PASS,DB)

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if($_SERVER['REQUEST_METHOD']=='GET'){

    $id  = $_GET['id'];


$sql = "SELECT * FROM user WHERE id='".$id."'";

if ($result = mysqli_query($link, $sql)) {
   printf("Select returned %d rows.\n", mysqli_num_rows($result));


}

$r = mysqli_query($con,$sql);

$res = mysqli_fetch_array($r);

$result = array();

array_push($result,array(
    "name"=>$res['name'],
    "username"=>$res['username'],
    "email"=>$res['email'],
                "age"=>$res['age'],
                "password"=>$res['password'],
    )
);

echo json_encode(array("result"=>$result));

mysqli_close($con);

}

This should help you to determine 1) if there is a problem with the database connection, and 2) if your query is returning results.