简体   繁体   中英

Select last N rows from MySQL - codeigniter

I'm using codeigniter ;

I would like to get last N rows from my table.

In my query I want to get last 200 rows:

         $this->m_general->select('count(*)');
         $this->m_general->from('pm');
         $result_count_query = $this->m_general->get();
         $count_query = $result_count_query->num_rows();



$data['all']     = $this->m_general->get('pm', array( 'admin_delete'=>0 ) , $count_query-200,$count_query, array('admin_seen'=>'asc' , 'id'=>'desc') );

but it returns nothing.

where is my wrong ?

updated

below query not worked fine and it returns all records :

$data['all']     = $this->m_general->get('pm', array( 'admin_delete'=>0 ) ,200, array('admin_seen'=>'asc' , 'id'=>'desc') );

Check out the API

https://ellislab.com/codeigniter/user-guide/database/active_record.html

Looks like you can't do this with the get method. Build your query according to the API.

$this->m_general->limit(200);
$this->m_general->order_by("admin_seen", "asc");
$this->m_general->order_by("id", "desc"); 

$data['all']     = 
$this->m_general->get('pm', array( 'admin_delete'=>0 ));

This Solve the Problem

$query = $this->db->query("SELECT * FROM pm WHERE admin_delete= 0 AND admin_seen=0 ORDER BY id DESC LIMIT  200");
$result = $query->result_array();
$count = count($result);

if(empty($count))
{
    echo 'array is empty';
}
else{
    return $result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM