代码点火器活动记录仅选择第一条记录,而不是最新的记录


Codeigniter Active Record is only selecting the first record instead of the most recent

我的问题很简单。我正在使用代码点火器活动记录,模型类所要做的就是从属于用户的表中选择最新的项目。

function get_progress($users_id)
 {
  $query = $this->db->get('progress');
  $this->db->where('user_key', $users_id);
  $this->db->order_by("id", "desc");
  $this->db->limit(1);
  return $query->row_array();
 }

看起来很简单,但由于某种原因,它正在获取与user_key匹配的最低 id。

我尝试将 where 语句更改为

  $this->db->where('id', '2');

它有效,但当然,这只是为了故障排除。我需要变量。

我已经重写了几种方法,包括使用 get_where() 和将 desc 更改为 asc。无论如何,它都在抓住低id。如何选择最高 id,其中user_key是匹配的数字。

你可以试试下面的代码

function get_progress($users_id){
  return $this->db->from('progress')
  ->where('user_key', $users_id)
  ->order_by("id", "DESC")
  ->get()
  ->row();
}

您将获得最后一行作为 STD 类对象

function get_progress($users_id)
{
        $this->db->select('*')
        ->from('progress')
        ->where('user_key',$users_id)
        ->order_by('id','desc')
        ->limit(1);
        $q=$this->db->get();
        return $q->result_array();
}