How to get last record from MySQL Database table in Codeigniter?
In this post we will give you information about How to get last record from MySQL Database table in Codeigniter?. Hear we will give you detail about How to get last record from MySQL Database table in Codeigniter?And how to use it also give you demo for it if it is necessary.
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”.
Hope this code and post will helped you for implement How to get last record from MySQL Database table in Codeigniter?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs