php 为什么 mysqli_insert_id() 总是返回 0?

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

Why is mysqli_insert_id() always returning 0?

phpmysql

提问by Mumbo Jumbo

I have the following code. The mysqli_insert_id() (in this case "$last_row"), which is supposed to return the last row of the table, is always returning 0. Why is it so?

我有以下代码。mysqli_insert_id()(在本例中为“$last_row”),它应该返回表的最后一行,总是返回 0。为什么会这样?

<?php
include('connect-db.php');
$submit_date = date("Ymd");
$content = mysqli_real_escape_string($connection, htmlspecialchars($_POST['editor']));
$ip_address = $_SERVER['REMOTE_ADDR'];
$last_row = mysqli_insert_id($connection);

if ($content != '') {

$sql = "INSERT INTO source ( track_id, submit_date, ip, content) VALUES ('$last_row', '$submit_date','$ip_address','$content')";

if (!mysqli_query($connection,$sql))
  {
  die('Error: ' . mysqli_error($connection));
  }

echo $last_row;
mysqli_close($connection);
}

?>

回答by T.J. Crowder

mysqli_insert_iddoes notreturn the ID of the last row of the table. From the docs, it:

mysqli_insert_id没有返回表的最后一排的ID。从文档中,它:

...returns the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute. If the last query wasn't an INSERTor UPDATEstatement or if the modified table does not have a column with the AUTO_INCREMENTattribute, this function will return zero.

...返回对具有 AUTO_INCREMENT 属性的列的表的查询生成的 ID。如果最后一个查询不是INSERTorUPDATE语句,或者如果修改后的表没有具有该AUTO_INCREMENT属性的列,则此函数将返回零

(My emphasis)

(我的重点)

That is, if you were to run it immediately afteran insert that auto-generated an ID, on the same connection you did the insert with, it would return the ID generated for that insert.

也就是说,如果您在自动生成 ID 的插入之后立即运行它,在您进行插入的同一个连接上,它将返回为该插入生成的 ID。

This is illustrated by the example in the docs linked above:

上面链接的文档中的示例说明了这一点:

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

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

回答by Sudip Pal

To get the result, you should place the

要获得结果,您应该将

$last_row = mysqli_insert_id($connection);

after your INSERT query

在您的 INSERT 查询之后

回答by PJ Brunet

For other people coming here, maybe you tried INSERT IGNORE INTOand you have a UNIQUEvalue that was already inserted. In that case, this id is zero.

对于来到这里的其他人,也许您尝试过INSERT IGNORE INTO并且您UNIQUE已经插入了一个值。在这种情况下,此 id 为零。

Also you'll get "zero" if MySQL runs out of connections. I'm no expert on persistent connections but this might help somebody?

如果 MySQL 连接用完,您也会得到“零”。我不是持久连接方面的专家,但这可能对某人有所帮助?

As you probably know PHP “mysql” extension supported persistent connections but they were disabled in new “mysqli” extension --Peter Zaitsev

您可能知道 PHP “mysql” 扩展支持持久连接,但它们在新的 “mysqli” 扩展中被禁用 --Peter Zaitsev

回答by c00000fd

Another gotcha with this function -- if you're doing:

这个功能的另一个问题——如果你正在做:

INSERT INTO table (col1, col2) VALUES (1, 2) ON DUPLICATE KEY UPDATE col2=3

and insert doesn't happen because of a duplicate key and for the UPDATE, like in my case, if col2was already set to 3, then mysqli_insert_idwill also return 0.

并且插入不会因为重复键而发生,并且对于UPDATE,就像在我的情况下一样,如果col2已经设置为3,那么mysqli_insert_id也将返回 0。