I'm trying to parse a JSON string. Before parsing I put my data in JSON format, but I am getting a "trying to get property of non-object" error.
Here is my code:
$querys = "SELECT * FROM image_refrences WHERE building_id=''";
$rows = array();
$responce= array();
$data = $conn->query($querys);
while($row = $data->fetch_assoc()){
$json['images'][] = array('id' => $row['id'],
'url' => $row['image_file'],
'location' => $row['location'] );
}
$responce= json_encode($json, TRUE);
$rows=json_decode($responce,TRUE);
foreach ( $rows->images as $output ) {
echo $output->id;
}
My JSON string will look like this:
{"images":[
{"id":"1","url":"def6a9.jpg","location":""},
{"id":"2","url":"def6a9.jpg","location":""},
{"id":"3","url":"fullsize_distr.jpg","location":""}
]}
Can someone help me find what I'm doing wrong?
json['images']is not valid PHP syntax, variables need to start with$.json_encode()is supposed to be an integer containing flags. Why are you usingTRUE?