silvia zulinka
silvia zulinka

Reputation: 731

select * from table where id = select max id in codeigniter

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

Answers (4)

silvia zulinka
silvia zulinka

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

Md.Jewel Mia
Md.Jewel Mia

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

Gopal Bhuva
Gopal Bhuva

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

Sunny Khatri
Sunny Khatri

Reputation: 535

Try this

$this->db->select('id, month, year')->order_by('id','desc')->limit(1)->get('tb_invoice')->row('id');

Upvotes: 2

Related Questions