2017-03-09 75 views
0

直升機,無法選擇數據正確

我寫了一個查詢我要顯示的用戶列表,其中該當前用戶沒有遵守我想這樣做,但它顯示我只有2個記錄,並在我刪除限制它顯示我所有的用戶,除了我已經跟隨它的一個是好的,但我不知道爲什麼它只顯示2個用戶,當我把限制,因爲它應該是5前3個用戶不顯示是它因爲我跟在他們後面,這裏是我的腳本,我無法正確地解釋,但我將不勝感激您的意見,所以我可以更多的相應解釋

<?php 
$sql = $this->db->limit(5); 
$sql = $this->db->get('users'); 
foreach($sql->result() as $data): 
$sql1 = $this->db->get_where('followers', array('user_id' => $data->id)); 
$sql2 = $this->db->get_where('followers', array('user_id' => $data->id, 'follower_id' => $user_id)); 
if($sql2->num_rows() != 0): 
echo $data->username; 
endif; 
endforeach; 
?> 

表S = tructure追隨者

id(int)  
user_id(int)   
follower_id(int) 

表結構的用戶

id(int)  
username(varchar)   
email(varchar) 
password(varchar) 
+0

你可以發表你的表模式? –

+0

@NavjotSingh用戶或追隨者? –

+0

無論是用戶還是關注者 –

回答

0
<?php 
    $this->db->start_cache(); 
    $sql = $this->db->limit(5); 
    $this->db->stop_cache(); 
    $sql = $this->db->get('users'); 
    $this->db->flush_cache(); 

    foreach($sql->result() as $data): 
    $this->db->start_cache(); 
    $sql1 = $this->db->get_where('followers', array('user_id' => $data->id)); 
    $this->db->flush_cache(); 

    $this->db->start_cache(); 
    $sql2 = $this->db->get_where('followers', array('user_id' => $data->id, 'follower_id' => $user_id)); 
    $this->db->flush_cache(); 

    if($sql2->num_rows() != 0): 
    echo $data->username; 
    endif; 
    endforeach; 
?>