[英]Getting ID of last insert
I am implementing an auto save feature which saves every minute on a form that is being edited or created. 我正在实现一种自动保存功能,该功能将每分钟保存在正在编辑或创建的表单上。
I am doing an insert of a null
value beforehand in an auto-incremented db table. 我正在自动增量数据库表中预先插入null
值。 From here after the null
insert, I need to retrieve the id
that was inserted so that I can update off of that ID. 从此处插入null
之后,我需要检索插入的id
,以便可以从该ID更新。 How Can I go along of getting this ID with the code I have provided? 如何通过提供的代码获取此ID?
I have tried using mysqli_insert_id
as shown below, but no luck. 我已经尝试使用mysqli_insert_id
,如下所示,但是没有运气。
// insert null into database to create the save point
if ($stmt = $mysqli->prepare("INSERT INTO test_table (name, email, notes) VALUES ('x', 'x', 'x')"))
{
$stmt->execute();
$stmt->close();
}
// show an error if the query has an error
else
{
echo "ERROR: Could not prepare Auto-Save SQL statement.";
}
//GET LAST ID
printf("Last inserted record has id %d\n", mysqli_insert_id());
Posting as a community wiki . 作为社区Wiki发布。
printf ("New Record has id %d.\n", mysqli_insert_id($mysqli));
You didn't pass your db connection to it. 您没有将数据库连接传递给它。
Reference: 参考:
Object oriented style 面向对象的风格
mixed $mysqli->insert_id; 混合$ mysqli-> insert_id;
Procedural style 程序风格
mixed mysqli_insert_id ( mysqli $link ) 混合mysqli_insert_id(mysqli $ link)
//this query will get the auto increment value of the specific table //此查询将获取特定表的自动增量值
SELECT `AUTO_INCREMENT`
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbname'
AND TABLE_NAME = 'tblname';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.