How to get last insert ID of MySQL table in PHP?
P粉504080992
P粉504080992 2023-08-23 21:49:21
0
2
523
<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>
P粉504080992
P粉504080992

reply all(2)
P粉973899567

There is a function that can know what the last id inserted in the current connection is

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

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

P粉895187266

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 .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!