In this PHP Codeigniter Tutorial, I will tell you how to get last record from the MySQL database table.
This is very simple to get last inserted rows from the table in Codeigniter. Normally I use order by clause along with limit 1 to get last row from the table using MySQL query and same you can do in Codeigniter. In Codeigniter, you can write either MySQL raw query or you can use
order_by() function. You will define the column name in the first parameter and in second parameter you will set the direction("ASC", "DESC") of the result.
get() method is used to retrieve all records from a table and
limit() method is used to define the number of rows you would like to returned by the query.
Ok, find the below query to get last record from the table :
$this->load->database(); $last_row = $this->db->order_by('id',"desc") ->limit(1) ->get('countries') ->row(); print_r($last_row);
For this example, I have a table "countries".How to get last record from table in PHP Laravel Framework?