Joomla!

How to get the last Inserted Id using the Joomla! Platform

Written by Ian Carnaghan · 17 sec read >

After you performed an Insert statement use the insertid method to get the auto-incremented value from the last Insert statement

$db = $this->getDbo();
$query  = $db->getQuery(true);
 $query->clear();
$query->insert('#__database_table1');
$query->set("id=".(int)$id);
$db->setQuery($query);
 $db->query();
 
//Now we get the last inserted id
$lastInsertId = $db->insertid();
 
echo $lastInsertId;

Note in the above code we used $db->insertid(); to obtain the last inserted id.

Last Updated On March 25, 2018
Written by Ian Carnaghan
I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment. In my spare time I teach undergraduate classes in web development. Profile

Leave a Reply

avatar
  Subscribe  
Notify of