Abin Jacob Abin Jacob - 3 months ago 10
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";

$result=mysql_query($query);

$value = mysql_num_rows($result);

if($value>=1)
{
while($row = mysql_fetch_array($result))
{

$details = array(
'status'=>'sucess',
'message'=>'address available',
'id' => $row['id'],
'customer_id' =>$row['customer_id'],
'at' => $row['at'],
'name'=>$row['name'],
'mobile'=>$row['mobile'],
'city'=>$row['city'],
'address'=>$row['address'],
'latlog'=>$row['latlog'],
'default'=>$row['default']
);
}
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

Please remove while i think no need of while

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

$result=mysql_query($query);

$value = mysql_num_rows($result); 

$json=array();
        $json['status']='sucess';
        $json['message']='address available';
        $json['details']=$value;

        echo jsone_encode($json);
Comments