CodeIgniter Forums
How do I get the autoincremented ID of the latest inserted row. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: How do I get the autoincremented ID of the latest inserted row. (/showthread.php?tid=84146)



How do I get the autoincremented ID of the latest inserted row. - davecoventry - 10-15-2022

My code:
PHP Code:
    $this->db->simpleQuery('Use D'.$id);
    $this->db->table('drawings')->insert(array("name"=>$fname));
    return $this->db->insertID(); 

This returns a model object, but I need the number of the ID.
if I use
PHP Code:
$this->db->insert_id(); 

I get an error which says that insert_id() is an undefined method.

Code:
+----------+--------------+------+-----+---------------------+----------------+
| Field    | Type        | Null | Key | Default            | Extra          |
+----------+--------------+------+-----+---------------------+----------------+
| id      | int(9)      | NO  | PRI | NULL                | auto_increment |
| name    | varchar(255) | YES  |    | NULL                |                |
| revision | char(3)      | NO  |    | NULL                |                |
| date    | timestamp    | NO  |    | current_timestamp() |                |
+----------+--------------+------+-----+---------------------+----------------+



RE: How do I get the autoincremented ID of the latest inserted row. - JustJohnQ - 10-15-2022

PHP Code:
$this->db->insertID() 
It returns an integer here...


RE: How do I get the autoincremented ID of the latest inserted row. - davecoventry - 10-16-2022

It's suddenly started working for me too.

I have no idea what I was doing wrong, but it was obviously something stupid.

Thanks for your response, and sorry to have wasted everybody's time (including my own!).