[英]Retrieve last inserted id with Mysql
再会,
我愿意在 Mysql 中检索新插入的行的 id 值。
我知道有 mysqli_insert_id 函数,但是:
我不想冒险去查询最高的 id,因为有很多查询,它可能会给我错误的...
(我的 id 列是自动递增的)
https://github.com/mysqljs/mysql#getting-the-id-of-an-inserted-row很好地描述了解决方案:
connection.query('INSERT INTO posts SET ?', {title: 'test'}, function(err, result, fields) {
if (err) throw err;
console.log(result.insertId);
});
var table_data = {title: 'test'};
connection_db.query('INSERT INTO tablename SET ?', table_data , function(err, result, fields) {
if (err) {
// handle error
}else{
// Your row is inserted you can view
console.log(result.insertId);
}
});
您也可以访问此链接查看https://github.com/mysqljs/mysql#getting-the-id-of-an-inserted-row
我认为您误解了mysqli_insert_id()
方法的使用。 该方法必须在插入语句后立即执行,以获取最后插入的 id。 如果你直接做我的 MySQL
INSERT INTO(a,b)values(1,'test');
SELECT LAST_INSERT_ID(); -- this will display the last inserted id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.