I have written PHP code to convert query result to JSON.
But while using jSON encode, my result only shows field values. There is no field name in the resulting json file.
My code:
$sql = 'select * from "'.$tablename.'"';
$myarray = array();
while ($row = pg_fetch_row($ret)) {
$myarray[] = $row;
}
$jsonData = json_encode(array('data' => $myarray));
Result:
{"data":[["Bob", "23", "New York"], ["Alice", "20", "Sidney"], ....]}
Expected Result:
{"data":[
{"firstName":"Bob", "age":"23", "City":"New York"},
{"firstName":"Alice", "age":"20", "City":"Sidney"},...]}