How to get last insert ID of MySQL table in PHP?
P粉504080992
2023-08-23 21:49:21
<p>I have a table into which new data is frequently inserted. I need to get the last ID of the table. How can I do this? </p>
<p>Is it similar to <code>SELECT MAX(id) FROM table</code>? </p>
There is a function that can know what the last id inserted in the current connection is
Plus using max is a bad idea as it may cause problems if your code is used simultaneously in two different sessions. .
The function is called mysql_insert_id
If you are using PDO, use
PDO::lastInsertId
.If you are using Mysqli, please use
mysqli::$insert_id
.If you are still using Mysql:
But if you must, use
mysql_insert_id
.