Reputation: 731
How to get data where id = last id in table and show all value not one value ?
I have table name=tb_invoice and 3 column (id, month, year) How to get last id and show result (id, month, year) in codeigniter? here my code :
$this->db->select_max('id');
$query = $this->db->get('tb_invoice');
return = $query->row_array();
the result just show id, how to show all value (id, month, year)?
Upvotes: 2
Views: 10616
Reputation: 731
thanks to @Sunny Khatri for code, this is worked:
$this->db->limit(1);
$this->db->order_by('id', 'DESC');
$query = $this->db->get('tb_invoice');
return $query->row_array();
Upvotes: 0
Reputation: 3441
Please Try this:
$maxid = $this->db->query('SELECT MAX(id) AS `maxid` FROM `tb_invoice`')->row()->maxid;
UPDATE
This will work even if your table is empty (unlike my example above):
$maxid = 0;
$row = $this->db->query('SELECT MAX(id) AS `maxid` FROM `tb_invoice`')->row();
if ($row) {
$maxid = $row->maxid;
}
Upvotes: 0
Reputation: 654
Try using insert_id.
$this->db->insert_id();
$query = $this->db->get('tb_invoice');
return = $query->row_array();
Or you can use
$this->db->select_max('{primary key}');
$query = $this->db->get('tb_invoice');
return = $query->row_array();
Upvotes: 0
Reputation: 535
Try this
$this->db->select('id, month, year')->order_by('id','desc')->limit(1)->get('tb_invoice')->row('id');
Upvotes: 2