I have a table, which regularly is inserted with new data.
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, which regularly is inserted with new data.
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
.
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
It's ok to use mysql_insert_id(), but there is one specific note about using it, you must call it after executed INSERT query, means in the same script session. If you use it otherwise it wouldn't work correctly.