⇧
vadnica-logo
My Services About the Project Telegram O meni O meni

Retrieving the ID with the lastInsertId() Method

The lastInsertId() method returns the ID of the last inserted row or a sequence value automatically generated by the database (typically via AUTO_INCREMENT). This is crucial in situations where, after inserting data into one table, you need this unique ID to link with other tables in the database. The method operates directly on the connection object rather than on a prepared statement.

EXAMPLE
RESULT
The result will be displayed here...

Practical Application: We call this method immediately after a successful INSERT command. If the insertion was unsuccessful or if the table does not have automatic key generation enabled, the method will return 0 or the string "0". Check the process at home by following these steps: