1

Ok so this is the code I have in the php file. I basically want to read this all in

    //Read the username and password from ServerRequest class
$username = $_POST["username"];   

    //Select all rows when the username match the input
$statement = mysqli_prepare($con, "SELECT * FROM Chore WHERE username = ?");
mysqli_stmt_bind_param($statement, "s", $username);
mysqli_stmt_execute($statement);

    //store the results of the previous query and create a variable for each column returned
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement,$chore_name ,$point_value, $parent_username, $child_username, $created, $points_given);
$chore = array();

    //store the previous variables you created in the array (only the ones you want to use)
while(mysqli_stmt_fetch($statement)) {
    $chore['chore_name'] = $chore_name;
    $chore['child_username'] = $child_username;
        $chore['point_value'] = $point_value;
    }
    //encode the array in JSON format
echo json_encode($chore);

//close the statement 
mysqli_stmt_close($statement);

    //Close the database connection 
mysqli_close($con);

I am wondering how would I read each entry to display as a list view in android studio?

1 Answers1

1

You are over writing the $chore array occurances in the loop.

Instead, create a temp array with the contents of the fetch, and add that to a new occurance of the $chore array.

//store the previous variables you created in the array (only the ones you want to use)
while(mysqli_stmt_fetch($statement)) {
    $t = array('chore_name'     => $chore_name,
               'child_username' => $child_username,
               'point_value'    => $point_value
              );
    $chore[] = $t;
}
//encode the array in JSON format
echo json_encode($chore);

Now you should see an array of arrays in your java code on the andriod device

Personally I would make an array of objects like this

while(mysqli_stmt_fetch($statement)) {
    $t = new stdClass();
    $t->chore_name     = $chore_name;
    $t->child_username = $child_username;
    $t->point_value    = $point_value;

    $chore[] = $t;
}

Then java will see an array of objects.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149