gpt4 book ai didi

php - 获取最后插入的 ID 数组 mysql php

转载 作者:行者123 更新时间:2023-11-29 06:37:42 25 4
gpt4 key购买 nike

我有一个查询插入到 tbl(str)values('a'),('b'),('c')

如果我只有一个插入,那么通过使用 mysqli_insert_id($con) 我可以插入最后一个 ID,但是如何在这个多个插入查询中插入所有 ID?

最佳答案

last_insert_id() 的这种行为是 documented in the MySQL docs:

The currently executing statement does not affect the value of LAST_INSERT_ID(). Suppose that you generate an AUTO_INCREMENT value with one statement, and then refer to LAST_INSERT_ID() in a multiple-row INSERT statement that inserts rows into a table with its own AUTO_INCREMENT column. The value of LAST_INSERT_ID() will remain stable in the second statement; its value for the second and later rows is not affected by the earlier row insertions. (However, if you mix references to LAST_INSERT_ID() and LAST_INSERT_ID(expr), the effect is undefined.)

如果你真的需要它,你可以使用带有 array_push 的 foreach 来测试它

 <?php

$InsetQueryArray = array(
"Insert into tbl(str) values('a')",
"Insert into tbl(str) values ('b')",
"Insert into tbl(str) values('c')"
);

$allLasIncrementIds = array();

foreach ($InsetQueryArray as $value) {

//execute it mysql
//Then use array_push

array_push($allLastIncrementIds, mysqli_insert_id($con));

}

?>

关于php - 获取最后插入的 ID 数组 mysql php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23443987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com