美好的一天,
我愿意在Mysql中检索刚插入的行的id值.
我知道有mysqli_insert_id函数,但是:
我不想冒险查询最高的ID,因为有很多查询,它可能会给我错误的...
(我的id列是自动增量)
luk*_*sch 106
https://github.com/mysqljs/mysql#getting-the-id-of-an-inserted-row 非常好地描述了解决方案:
connection.query('INSERT INTO posts SET ?', {title: 'test'}, function(err, result) {
if (err) throw err;
console.log(result.insertId);
});
Run Code Online (Sandbox Code Playgroud)
编辑:拼写和在github中移动的repo,所以我改为当前链接.
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);
}
});
Run Code Online (Sandbox Code Playgroud)
您也可以通过访问以下链接进行查看:https://github.com/mysqljs/mysql#getting-the-id-of-an-inserted-row
归档时间: |
|
查看次数: |
58964 次 |
最近记录: |