# How to get last inserted record?

**URL:** <https://forums.percona.com/t/how-to-get-last-inserted-record/677>\
**Category:** Other MySQL® Questions\
**Created:** [March 17, 2008, 10:36pm UTC](https://forums.percona.com/t/how-to-get-last-inserted-record/677 "2008-03-17T22:36:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rojavenkat](https://avatars.discourse-cdn.com/v4/letter/r/41988e/32.png) [@rojavenkat](https://forums.percona.com/u/rojavenkat)\
**Post date:** [March 17, 2008, 10:36pm UTC](https://forums.percona.com/t/how-to-get-last-inserted-record/677/1 "2008-03-17T22:36:40Z")

</div>

Hi All,  
Please tell how to get last inserted record from the table in PHP,Mysql.  
Please reply me.  
Thanks,  
Roja

---

<div class="post-metadata">

**Author:** ![debug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/debug/32/772_2.png) [@debug](https://forums.percona.com/u/debug)\
**Post date:** [March 18, 2008, 12:43am UTC](https://forums.percona.com/t/how-to-get-last-inserted-record/677/2 "2008-03-18T00:43:09Z")

</div>

Roja,

It is LAST\_INSERT\_ID().

Common usage case:

INSERT INTO foo (auto,text)  
VALUES(NULL,‘text’); # generate ID by inserting NULL  
INSERT INTO foo2 (id,text)  
VALUES(LAST\_INSERT\_ID(),‘text’); # use ID in second table

Please see more details in MySQL documentation if needed:  
[http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.htm](http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.htm) l
