如何在 PHP 中获取 MySQL 表的最后插入 ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1685860/
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:30:48  来源:igfitidea点击:

How do I get the last inserted ID of a MySQL table in PHP?

phpmysql

提问by gweg

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?

我有一个经常插入新数据的表。我需要获取表的最后一个 ID。我怎样才能做到这一点?

Is it similar to SELECT MAX(id) FROM table?

它类似于SELECT MAX(id) FROM table?

回答by deceze

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

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

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

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

If you're still using Mysql:

如果您仍在使用 Mysql:

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 statementsinstead, and use PDOor MySQLi- this articlewill help you decide which. If you choose PDO, here is a good tutorial.

请不要mysql_*在新代码中使用函数。它们不再被维护并被正式弃用。看到红框了吗?了解准备好的语句,并使用PDOMySQLi-本文将帮助您决定哪个。如果您选择 PDO,这里有一个很好的教程

But if you have to, use mysql_insert_id.

但如果必须,请使用mysql_insert_id.

回答by RageZ

there is a function to know what was the last id inserted in the current connection

有一个函数可以知道当前连接中插入的最后一个 id 是什么

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

plus using maxis a bad ideabecause it could lead to problemsif your code is used at same time in two different sessions.

加上使用max是一个坏主意,因为如果您的代码在两个不同的会话中同时使用,它可能会导致问题

That function is called mysql_insert_id

该函数称为mysql_insert_id

回答by x2.

It's ok. Also you can use LAST_INSERT_ID()

没关系。你也可以使用LAST_INSERT_ID()

回答by Naftali aka Neal

With PDO:

使用PDO

$pdo->lastInsertId();

With Mysqli:

使用Mysqli

$mysqli->insert_id;


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 statementsinstead, and use PDOor MySQLi- this articlewill help you decide which. If you choose PDO, here is a good tutorial.

请不要mysql_*在新代码中使用函数。它们不再被维护并被正式弃用。看到红框了吗?了解准备好的语句,并使用PDOMySQLi-本文将帮助您决定哪个。如果您选择 PDO,这里有一个很好的教程

回答by dlamblin

What you wrote would get you the greatest idassuming 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.

id假设您编写的内容是唯一且自动递增的,那么您编写的内容将使您受益匪浅,假设您可以接受并发问题。
由于您使用 MySQL 作为数据库,因此有特定功能LAST_INSERT_ID()仅适用于执行插入的当前连接。
PHP 提供了一个特定的函数,也称为mysql_insert_id.

回答by Sandhu

Try this should work fine:

试试这个应该可以正常工作:

$link = mysqli_connect("localhost", "my_user", "my_password", "world");

$query = "INSERT blah blah blah...";
$result = mysqli_query($link, $query);

echo mysqli_insert_id($link);

回答by Rahul

You can get the latest inserted id by the in built php function mysql_insert_id();

您可以通过内置的 php 函数获取最新插入的 id mysql_insert_id();

$id = mysql_insert_id();

you an also get the latest id by

您还可以通过以下方式获得最新的 ID

$id = last_insert_id();

回答by Mihail Dimitrov

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.

可以使用mysql_insert_id(),但是有一个关于使用它的特定注意事项,您必须在执行 INSERT 查询后调用它,意味着在同一个脚本会话中。如果您使用它,否则它将无法正常工作。

回答by Narsimha

To get last inserted id in codeigniter After executing insert query just use one function called insert_id()on database, it will return last inserted id

在codeigniter中获取最后插入的id执行插入查询后只需使用一个insert_id()在数据库上调用的函数,它将返回最后插入的id

Ex:

前任:

$this->db->insert('mytable',$data);
echo $this->db->insert_id(); //returns last inserted id

in one line

一行

echo $this->db->insert('mytable',$data)->insert_id();

回答by Hitesh

Clean and Simple -

干净简单 -

$selectquery="SELECT id FROM tableName ORDER BY id DESC LIMIT 1";
$result = $mysqli->query($selectquery);
$row = $result->fetch_assoc();
echo $row['id'];