0

I want to retrieve data from user table using username but it cannot work properly here is my php code:

<?php

mysql_connect("xxx","xxx","xxx");
mysql_select_db("xxx");

$username = $_POST["name"];

$sql = mysql_query("select * from user where username = '$username'");

$flag["code"] = 0;

$check = mysql_fetch_row($sql);
if ($check > 0)
{
    $flag["code"] = 1;
    echo json_encode($flag);    

    while ($row= mysql_fetch_assoc($sql)) 
    {
        $output[] = $row;
    }
}

echo json_encode($output);
mysql_close();

?>

And here is my Java code:

  try
  {
     JSONObject json = new JSONObject(result);
     code = (json.getInt("code"));

if (code == 1)
{
    JSONArray array = new JSONArray(result);

    for (int i = 0; i < array.length(); i++)
    {
        JSONObject jsonObject = array.getJSONObject(i);
        firstname = jsonObject.getString("firstname");

        et.setText(firstname);
    }
}
}
catch (Exception e)
{

}

Here is result:

try
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8),8);
            StringBuilder sb = new StringBuilder();

            while ((line = reader.readLine()) != null)
            {
                sb.append(line+"\n");
            }

            is.close();

            result = sb.toString();

            Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

There is no error but it cannot retrieve any data what is the problem ?

5
  • Where is the part of result = ?? or what is the output of result? Commented Mar 19, 2015 at 1:54
  • Have you checked what JSON is returned by your PHP script? Commented Mar 19, 2015 at 1:54
  • i've just include the part of result and it says empty Commented Mar 19, 2015 at 2:10
  • here is the result {"code:1"} null Commented Mar 19, 2015 at 2:16
  • Whats var_dump($output); show? Commented Mar 19, 2015 at 2:19

2 Answers 2

1

Please return json object from php not Array i have channged echo part to json_encode(array('response'=>$output,'code'=>$flag["code"]));

<?php

mysql_connect("xxx","xxx","xxx");
mysql_select_db("xxx");

$username = $_POST["name"];

$rs = mysql_query("select * from user where username = '$username'");


$flag["code"] = 0;

$check = mysql_num_rows($rs);
if ($check > 0)
{
    $flag["code"] = 1;


    while ($row= mysql_fetch_array($rs)) 
    {
        $output[] = $row;
    }
}

echo json_encode(array('response'=>$output,'code'=>$flag["code"])); //changed here
mysql_close();

?>

Now in java,

try
  {
     JSONObject json = new JSONObject(result);
     code = (json.getInt("code"));

if (code == 1)
{
    JSONArray array = json.getJSONArray('response');

    for (int i = 0; i < array.length(); i++)
    {
        JSONObject jsonObject = array.getJSONObject(i);
        firstname = jsonObject.getString("firstname");

        et.setText(firstname);
    }
}
}
catch (Exception e)
{

}

If it fails JSONObject json = new JSONObject(result); is not required because result is already json object. just remove that line and change all json variables to result.

Sign up to request clarification or add additional context in comments.

8 Comments

you should be using mysqli over mysql. but good solution this will def tell him if hes getting results +1
Did not change anything response says null
can you tell me the type of result. String or JSONObject ?
The type of result is String and it gets JSONObject data which is converted into String
Same as before result is null :(
|
0

I've just solved the problem result didn't work in other words it cannot get data by array and i just put array function now it works fine :-)

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.