It might flow like this: prepare your insert statement and get all your data ready, lock tables execute your statement and insert the new data retrieve the last id (which will be from what you just inserted) with a select query unlock tables do stuff with your id