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 table
?
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 table
?
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
.Try this should work fine:
It's ok. Also you can use LAST_INSERT_ID()
It's sad not to see any answers with an example.
Using Mysqli::$insert_id:
Using PDO::lastInsertId:
You can get the latest inserted id by the in built php function
mysql_insert_id();
you an also get the latest id by
If your table have AUTO INCREMENT column like UserID,Emp_ID,.. then you can use this query to get last inserted record SELECT * FROM table_name where UserID=(select MAX(UserID)from table_name) In PHP code:
Then you can use fetched data as your requirement