0

I am working on an android app project which uses this php script to retrieve the data from MySql database:

<?php
$con=mysqli_connect("localhost","root","","webservice");

$item_name = "Navnita";


$statement = mysqli_prepare($con, "SELECT * FROM reviews WHERE item_name = ?");
mysqli_stmt_bind_param($statement, "s", $item_name);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $item_name, $username, $review);

$revs = array();

while(mysqli_stmt_fetch($statement)){
    $revs["item_name"] = $item_name;
    $revs["username"] = $username;
    $revs["review"] = $review;              
}

echo json_encode($revs);
mysqli_close($con);

?> I wanted to retrieve all the rows having item_name = "Navnita" but it only retrieves one row:

{"item_name":"Navnita","username":"Jenny","review":"Food is just great!"}

The database contains 3 rows having item_name = "Navnita". Please help me out guys! I am not that good at php and database as well :|

1
  • $revs["item_name"][] = $item_name; all your data add [] Commented Sep 19, 2015 at 7:45

2 Answers 2

0

For that you need two arrays. Most logical is to store each row in one array and than store this array into another like:

$revs = array();
$allrevs = array();

while(mysqli_stmt_fetch($statement)){
    $revs["item_name"] = $item_name;
    $revs["username"] = $username;
    $revs["review"] = $review;       
    $allrevs[] = $revs;       
}

echo json_encode($allrevs);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Bas van Stein, that's what i was looking for.
-1

You are assigning values on same variable , as they are not appended they are overwriting them . Do as below

$revs = array();

while(mysqli_stmt_fetch($statement)){
   array_push( $revs,array("item_name"=>$item_name,"username"=>$username,"review"=>$review));   

}

It will give your desired result.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.