From the code below, this what I'm trying to achieve: If citizenID found by the $get_friends query are 1 and 2. I would like the while loop to first get all the rows for 1 and then do the same for 2. The code I currently have below its only retrieving the last row for 1 and 2. Please help.
<?php
include ('session_start.php');
include_once('db_connect_universal.php');
$user_id = $_SESSION['sess_id'];
//GET USER FRIENDS AND DETAILS
$get_friends = "SELECT * FROM citizens
INNER JOIN user_details
ON citizens.citizenID=user_details.UserID
WHERE citizens.userID = '".$user_id."'";
$results_get_friends = $conn->query($get_friends);
$num_rows_friends = mysqli_num_rows ($results_get_friends);
while ($row_friends = $results_get_friends->fetch_assoc()) {
$citizenID = $row_friends['citizenID'];
//GET RATINGS AND COMMENTS
$sql = "SELECT * FROM comments
INNER JOIN organiser ON comments.movieID=organiser.movieID
WHERE comments.userID= '".$citizenID."'
AND ratings_n_comments.Time BETWEEN DATE_SUB(CURDATE(), INTERVAL 28 DAY)
AND DATE_ADD(CURDATE(), INTERVAL 10 DAY)
ORDER BY comments.Time DESC";
$result = $conn->query($sql);
$num_rows = mysqli_num_rows ($result);
$row = $result->fetch_assoc();
$n_Rating[]= array(
'dp' => $row_friends['display_pics'],
'uname' => $row_friends['Uname'],
'poster'=> $row['mooster'],
'title'=> $row['moitle'],
'genre'=> $row['moenre'],
'comment'=> $row['Comment'],
'mid'=> $row['mID'],
'check' => 'data'
);
}
header('Content-Type: application/json');
echo json_encode ($n_Rating);
}
//detailed error reporting
if (!$sql) {
echo 'MySQL Error: ' . mysqli_error($conn);
exit;
}
?>