Codeigniter Active Record selects only the first record, not the most recent

My problem is pretty simple. I am using the active codeigniter entry, and the whole model class must do is select the very last item from the table owned by the user.

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();
 }

Seems simple, but for some reason, it captures the lowest id that matches user_key.

I tried changing the where statement to

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

And it works, but of course it's just for troubleshooting. I need variables.

I rewrote several ways, including using get_where () and changing desc to asc. No matter what, he grabbed a low id. How can I choose the highest identifier, where user_key is the corresponding number.

+4
2

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

STD

+1
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();
}
+1

Source: https://habr.com/ru/post/1569105/


All Articles