gpt4 book ai didi

php - mysqli->更新时插入_id (PHP)

转载 作者:搜寻专家 更新时间:2023-10-31 22:04:05 24 4
gpt4 key购买 nike

它对任何人都有效吗? :P

我可以在插入时正确获取 insert_id,但不能在更新时获取。当然 contactsId 列是 AUTO_INCREMENT。

完整代码:

<?php
$mysqli = new mysqli('localhost', [USER], [PASSWORD], [DB]);
$mysqli->set_charset("utf8");

$query = 'INSERT INTO contacts (contactsName) VALUES ("Mariola")';
$result = $mysqli->query($query);
echo $mysqli->insert_id . '<br />';

$query = 'UPDATE contacts SET contactsName = "Mariola" WHERE contactsId = 289';
$result = $mysqli->query($query);
echo $mysqli->insert_id;

输出:

1514
0

我有 ID 为 289 的记录,更新工作正常。

最佳答案

此行为在 document 中描述得非常清楚.

mysqli::$insert_id -- mysqli_insert_id — Returns the auto generated id used in the last query

If the last query wasn't an INSERT or UPDATE statement or if the modified table does not have a column with the AUTO_INCREMENT attribute, this function will return zero.

关于php - mysqli->更新时插入_id (PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22350282/

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