你好,
我写了一个查询,我想显示当前用户未关注的用户列表,我正在尝试这样做,但它只显示 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;
?>
表结构关注者
id(int)
user_id(int)
follower_id(int)
表结构用户
id(int)
username(varchar)
email(varchar)
password(varchar)
最佳答案
<?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;
?>
关于php - 无法正确选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42687584/