如何获取PHP中MySQL表的最后插入的ID? [英] How do I get the last inserted ID of a MySQL table in PHP?

查看:56
本文介绍了如何获取PHP中MySQL表的最后插入的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,经常在其中插入新数据.我需要获取表的最后一个ID.我该怎么办?

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?

SELECT MAX(id) FROM table相似吗?

推荐答案

如果您使用的是PDO,请使用 PDO::lastInsertId .

If you're using PDO, use PDO::lastInsertId.

如果您使用的是Mysqli,请使用 mysqli::$insert_id .

If you're using Mysqli, use mysqli::$insert_id.

如果您仍在使用Mysql:

If you're still using Mysql:

请不要使用mysql_*函数在新代码中 .它们已不再维护并已正式弃用.看到 红色框 ?改为了解 准备好的声明 ,并使用 MySQLi -本文将帮助您确定哪一个.如果选择PDO,这是一个很好的教程.

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

但是,如果需要,请使用 mysql_insert_id .

这篇关于如何获取PHP中MySQL表的最后插入的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆