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

后端 未结 16 2282
温柔的废话
温柔的废话 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条回答
  •  旧巷少年郎
    2020-11-21 23:41

    there is a function to know what was the last id inserted in the current connection

    mysql_query('INSERT INTO FOO(a) VALUES(\'b\')');
    $id = mysql_insert_id();
    

    plus using max is a bad idea because it could lead to problems if your code is used at same time in two different sessions.

    That function is called mysql_insert_id

提交回复
热议问题