php 如何从表 MySQL 中获取最后插入的 id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20891875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:39:06  来源:igfitidea点击:

How to get last inserted id from table MySQL

phpmysqldatabase

提问by Ronak Patel

I am running 1 script in php for that I need last inserted id in subscription table. By using that id I want to make notification note for that subscription.

我在 php 中运行 1 个脚本,因为我需要在订阅表中最后插入 id。通过使用该 ID,我想为该订阅制作通知说明。

I used:

我用了:

SELECT LAST_INSERT_ID() FROM subscription

I am getting 0 instead of real last inserted value.

我得到 0 而不是最后插入的真实值。

回答by Hamed Persia

If you use phpto connect to mysqlyou can use mysql_insert_id()to point to last inserted id.

如果您使用php连接到mysql您可以使用mysql_insert_id()指向最后插入的 id。

Like this :

像这样 :

mysql_query("INSERT INTO mytable (1, 2, 3, 'blah')");
$last_id = mysql_insert_id();

See this : mysql_insert_id()

看这个:mysql_insert_id()

回答by Binary Alchemist

LAST_INSERT_ID()returns the last id from a previous insert statement. If you want the most recently inserted record and are using Auto Increment Prime keys, you can use the code below:

LAST_INSERT_ID()返回前一个插入语句的最后一个 id。如果您想要最近插入的记录并使用 Auto Increment Prime 键,您可以使用以下代码:

SELECT MAX( id ) FROM subscription;

If you need to know what the NEXTid will be, you can get this from INFORMATION_SCHEMA

如果您需要知道NEXTid 是什么,您可以从 INFORMATION_SCHEMA 获取

SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'test'

mysql_insert_id

mysql_insert_id

回答by 1owk3y

This question has already been answered many times: MySQL: LAST_INSERT_ID() returns 0

这个问题已经回答过很多次了:MySQL: LAST_INSERT_ID() 返回 0

You are using that function out of context. It will only work if you inserted a row immediately prior thusly:

您正在脱离上下文使用该功能。它仅在您之前插入一行时才有效:

INSERT INTO 'subscription' (name) VALUES ('John Smith');
SELECT LAST_INSERT_ID() FROM subscription

You can however select the row with the highest id, which logically would be the most recently added...

但是,您可以选择具有最高 id 的行,这在逻辑上是最近添加的...

SELECT MAX( id ) FROM subscription;

The standard approach however is to simply call mysqli_insert_idor mysql_insert_id(depending on whether you are using the mysqli or mysql PHP library. I should add that the mysql library is very inadvisable to use since it is almost completely deprecated). Here's what the whole thing would ideally look like:

然而,标准方法是简单地调用mysqli_insert_idor mysql_insert_id(取决于您使用的是 mysqli 还是 mysql PHP 库。我应该补充一点,mysql 库非常不建议使用,因为它几乎完全被弃用了)。理想情况下,这就是整个事情的样子:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$mysqli->query("INSERT INTO 'subscription' (name) VALUES ('John Smith');");
printf ("New Record has id %d.\n", $mysqli->insert_id);
//Output is something like: New Record has id 999

If however you didn't insert a subscription in the same script as collecting the most recent row ID, use the 'select max' approach. This seems unlikely given that you mentioned '1 script'

但是,如果您没有在收集最新行 ID 的同一个脚本中插入订阅,请使用“select max”方法。鉴于您提到了“1 个脚本”,这似乎不太可能

Also, if your ID's are non-consecutive, or you do not have an ID field, or you have row ID's higher than the one you just added you should probably consider a 'date_added' column to determine which one was really the latest. These scenarios are rather unlikely however.

此外,如果您的 ID 不是连续的,或者您没有 ID 字段,或者您的行 ID 高于您刚刚添加的 ID,您可能应该考虑使用“date_ added”列来确定哪一个是最新的。然而,这些情况不太可能发生。

回答by Hemant Shori

this is the better approach 2 and 3 works but MAX(id) take more time to execute.

这是更好的方法 2 和 3,但 MAX(id) 需要更多时间来执行。

  1. SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'dbName' AND TABLE_NAME = 'tableName';

  2. SELECT tableName.id FROM tableName ORDER BY tableName.id DESC LIMIT 0,1;

  3. SELECT MAX( id ) FROM tableName;

  1. SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'dbName' AND TABLE_NAME = 'tableName';

  2. SELECT tableName.id FROM tableName ORDER BY tableName.id DESC LIMIT 0,1;

  3. SELECT MAX( id ) FROM tableName;

回答by chintan sheth

$last_id=mysql_query("SELECT id FROM `table_name` ORDER BY id DESC LIMIT 0 , 1" );
$row=mysql_fetch_assoc($last_id);
echo $row['id'];

Replace idby your id field name in database and table_nameby your table name.

替换id为您在数据库中的 id 字段名称和table_name您的表名。

回答by Mubo

This will always give you the maximum id, which says the biggest number is the last inserted one

这将始终为您提供最大 id,即最大的数字是最后插入的数字

 SELECT MAX(id) as MaximumID FROM subscription;