PHP/MySQL 插入后获取自增值

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

PHP/MySQL Get autoincremented value after insert

phpmysql

提问by clamp

In my mysql table i have an id-column which is set to autoincrement.

在我的 mysql 表中,我有一个设置为自动增量的 id 列。

then i do queries like this:

然后我做这样的查询:

INSERT INTO table (id, foo) VALUES ('', 'bar')

INSERT INTO table (id, foo) VALUES ('', 'bar')

how can i then safely find out which id was generated with this insert?

我怎样才能安全地找出用这个插入生成的 id?

if i just query the last id this might not be safe, since another insert could have happened in the meantime, right?

如果我只是查询最后一个 id,这可能不安全,因为在此期间可能发生了另一个插入,对吗?

采纳答案by sascha

There's a PHP and also a MySQL function for this: mysqli_insert_id()and PDO::lastInsertId().

这里有一个 PHP 和一个 MySQL 函数:mysqli_insert_id()PDO::lastInsertId().

http://php.net/manual/en/function.mysql-insert-id.php

http://php.net/manual/en/function.mysql-insert-id.php

回答by Emmanuel N

Use LAST_INSERT_ID()in SQL

在 SQL 中使用 LAST_INSERT_ID()

    SELECT LAST_INSERT_ID();

Use mysql_insert_id()in PHP

在 PHP 中使用 mysql_insert_id()

回答by RCNeil

If you are using PHP to get the auto_incremented value that is returned after an INSERT statement, try using the MySQLi insert_idfunction. The older mysql_insert_id()version is being deprecated in PHP.

如果您使用 PHP 获取 INSERT 语句后返回的 auto_incremented 值,请尝试使用MySQLiinsert_id函数。旧mysql_insert_id()版本在 PHP 中已被弃用。

An example below:

下面是一个例子:

 <?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$mysqli->query("CREATE TABLE myCity LIKE City");

$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
$mysqli->query($query);

printf ("New Record has id %d.\n", $mysqli->insert_id);

/* drop table */
$mysqli->query("DROP TABLE myCity");

/* close connection */
$mysqli->close();
?>

回答by Alfabravo

If you use mysql_query("..."), then mysql_insert_id()is the function you need. If you use something else to do queries, then the corresponding documentation should be checked

如果您使用 mysql_query("..."),那么mysql_insert_id()就是您需要的函数。如果你用别的东西做查询,那么应该查一下相应的文档

回答by Resad Indipa

Be Careful when using mysql_insert_id() specially if you have multiple connections to the Database. Because It doesn't get the value of the query you've inserted. it gets the latest id of the table. It may be a row another query has inserted. Only use this function if you access Database in one connection.

如果您有多个连接到数据库,则在使用 mysql_insert_id() 时要特别小心。因为它没有获得您插入的查询的值。它获取表的最新 id。它可能是另一个查询插入的行。仅当您在一个连接中访问数据库时才使用此功能。

https://www.php.net/manual/en/function.mysql-insert-id.php

https://www.php.net/manual/en/function.mysql-insert-id.php

回答by stimms

回答by Hobo

Since it's PHP, mysql_insert_idshould do the trick

由于它是 PHP,mysql_insert_id应该可以解决问题

回答by dynamic

mysqli_insert_id();

You can also simply run this query:

您也可以简单地运行此查询:

INSERT INTO table (foo) VALUES ('bar')

回答by Amigo

also in PDO after the execute command as the example below:

也在执行命令后的 PDO 中,如下例所示:

$lastId = $dbh->lastInsertId();

click for REF

点击参考