2017-02-07 283 views
0

從下面的代碼,這是我想要實現的:如果由$ get_friends查詢發現citizenID是1和2.我想要while循環首先獲取所有行1,然後對2進行同樣的操作。我現在的代碼僅在其下方檢索1和2的最後一行。請幫助。PHP不循環,因爲我想

<?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; 
} 
?> 
+0

明智代碼縮進使得調試代碼要容易得多 – RiggsFolly

+0

'if(!$ sql){'?? '$ sql'是一個包含查詢的字符串文字。不是發出查詢的結果。所以,你的錯誤報告,除了在這個過程中的方式太晚不會給你任何幫助,如報告錯誤 – RiggsFolly

+0

你似乎有不必要的'}' – RiggsFolly

回答

1

你只是取$row = $result->fetch_assoc();一行。

您是否試圖爲用戶提取所有評論?

因爲那麼你就需要循環槽一切$result有,就像你與while ($row_friends = $results_get_friends->fetch_assoc())

此外,而不是串聯您的SQL查詢做,用preparebind_param嘗試avoid possible SQL injection

+0

傻了,現在都很好,非常感謝jkrnak。同時感謝您對使用準備的建議和bind_param將會查看它。 – chiboz