CodeIgniter Forums
return insert id from database insert function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: return insert id from database insert function (/thread-76197.html)



return insert id from database insert function - cilux - 04-21-2020

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?!


RE: return insert id from database insert function - Leo - 04-21-2020

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();




RE: return insert id from database insert function - kilishan - 04-21-2020

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.