How to get last insert id after insert in Joomla database?

Get last id
Joomla solution for get last insert ID after insert a new row in J! database:


Example ↓

$query = $db->getQuery(true);
$last_id = $db->insertid();

In native PHP ↓
Object oriented style

$last_id = $mysqli->insert_id;

Procedural style

$last_id = mysqli_insert_id( mysqli $link )

Notes ↓

  • The mysqli_insert_id() function returns the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute.
  • If the last query wasn’t an INSERT or UPDATE statement or if the modified table does not have a column with the AUTO_INCREMENT attribute, this function will return zero.
  • Performing an INSERT or UPDATE statement using the LAST_INSERT_ID() function will also modify the value returned by the mysqli_insert_id() function.