I am trying to output JSON format result of post_table
in while loop, But I have to include an object value in the resulted array from fetching another table which is img_table
, here is my code:
$result_main = mysql_query("SELECT * FROM post_table WHERE userid='$userid' ");
$rowsm = array();
while($rm = mysql_fetch_assoc($result_main)) {
$row0 = mysql_fetch_array($result_main);
$user_post_id = $row0['user_post_id'];
$result=mysql_query("SELECT * FROM img_table WHERE post_id='$user_post_id' ");
while($row = mysql_fetch_array($result)) {
$user_img=$row['img_filename'];
$rm['img_filename'] = $user_img;
}
$rowsm[] = $rm ;
}
echo"{\"result_main\":";
print json_encode($rowsm);
echo '}';
But the result is incorrect. Is this the right way to do so?