CodeIgniter Forums

Full Version: return insert id from database insert function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have this function for insert data to database:
PHP Code:
    public function insertAlbumData(array $data)
    {
       $this->db->table('gallery_albums')
            ->insert($data);
    

now, how to return insertID from this function?!
now, how to return insertID from this function?!


PHP Code:
public function insertAlbumData(array $data)
{
   $this->db->table('gallery_albums')
         ->insert($data);
   return 
$this->db->insertID();

Pass true as the second argument.

PHP Code:
$id $this->db->table('gallery_albums')->insert($datatrue); 

Since there is overhead associated with getting the last inserted ID from many databases, you get a small performance boost by not returning it if you don't need it. So CI4 doesn't return that by default, but makes it easy to get it if you need it.