How do I get the last inserted ID of a MySQL table in PHP?

后端 未结 16 2324
温柔的废话
温柔的废话 2020-11-21 23:09

I have a table into which new data is frequently inserted. I need to get the very last ID of the table. How can I do this?

Is it similar to SELECT MAX(id) FROM

16条回答
  •  旧时难觅i
    2020-11-21 23:32

    What you wrote would get you the greatest id assuming they were unique and auto-incremented that would be fine assuming you are okay with inviting concurrency issues.
    Since you're using MySQL as your database, there is the specific function LAST_INSERT_ID() which only works on the current connection that did the insert.
    PHP offers a specific function for that too called mysql_insert_id.

提交回复
热议问题