0

I have one table. Which name is student. There is three column.

Name Address Roll Age
X      A      1   20
Y      B      2   20
z      C      3   20
M      D      4   25

I want to collect all name and address whose Age is 20. I use this.

$sql = "select Name,Age from student where Age = 20";

while($data= sqlsrv_fetch_array($sql,SQLSRV_FETCH_ASSOC))
 { 
   echo json_encode(data);
 }

By this i get just last row data that means

{"Name":X,"Address":A}

But i want like this

{"Name":X,"Address":A,"Name":Y,"Address":B,"Name":Z,"Address":C}
Mohibul Hasan Rana
  • 237
  • 2
  • 3
  • 16
  • Maybe this answer? http://stackoverflow.com/questions/383631/json-encode-mysql-results – jac Nov 05 '15 at 22:36

1 Answers1

0

You can try this code below, reference here:

$jsonData = array();
while ($array = mysql_fetch_row($sql,SQLSRV_FETCH_ASSOC)) {
    $jsonData[] = $array;
}
echo json_encode($jsonData);
Nguyễn Hải Triều
  • 1,454
  • 1
  • 8
  • 14