Abin Jacob Abin Jacob - 1 year ago 100
JSON Question

How to display Mysql php result json format?

my php code looks like:

$query = "SELECT *
FROM `address`
WHERE customer_id =$customer_id
ORDER BY `default` DESC";


$value = mysql_num_rows($result);

while($row = mysql_fetch_array($result))

$details = array(
'message'=>'address available',
'id' => $row['id'],
'customer_id' =>$row['customer_id'],
'at' => $row['at'],
echo json_encode($details);

Its output looks like:

{"status":"sucess","message":"address available","id":"52","customer_id":"14","at":"Home","name":"Shhsh","mobile":"99989998","city":"Calicut","address":"Gsggsgs","latlog":"76.3007429,76.3007429","default":"Yes"}

it is json object. My require json is:

{"status":"sucess","message":"address available","details":[{"id":"52","customer_id":"14","at":"Home","name":"Shhsh","mobile":"99989998","city":"Calicut","address":"Gsggsgs","latlog":"76.3007429,76.3007429","default":"Yes"}]

What all changes should I done to required json format? that is json object contain json array. what all changes should I done for getting required json format. I am new to this. Thanking in advance.

Answer Source

Please remove while i think no need of while

$query ="SELECT id,customer_id,at,name,mobile,
        FROM `address`
        WHERE customer_id =$customer_id
        ORDER BY `default` DESC";


$value = mysql_num_rows($result); 

        $json['message']='address available';

        echo jsone_encode($json);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download