i have following php code, storing last row of data query returns. how can store data in json array can access through android , populate list view?
<?php header('content-type: application/json '); $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'pass'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('could not connect: ' . mysql_error()); } $sql = 'select * tb_name'; mysql_select_db('db_name'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('could not data: ' . mysql_error()); } while($rows=mysql_fetch_assoc($retval)){ $json['cola'] = $rows['cola']; $json['colb'] = $rows['colb']; } echo json_encode($json); mysql_close($conn); ?>
the following code produces following json array data last row:
{ "cola": "contents of a", "colb": "contents of b" }
how can store rows returns in way keys separate so:
{ { "cola": "contents of a", "colb": "contents of b" }, { "cola": "contents of a", "colb": "contents of b" }, { "cola": "contents of a", "colb": "contents of b" } }
i know syntax might not right json file, thats want accomplish, missing?
you override data in while-loop. can this:
while($rows=mysql_fetch_assoc($retval)){ $row['cola'] = $rows['cola']; $row['colb'] = $rows['colb']; $json[] = $row; }