demo_db_insert_id.js:
var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", user: "myusername", password: "mypassword", database: "mydb" }); con.connect(function(err) { if (err) throw err; var sql = "INSERT INTO customers (name, address) VALUES ('Michelle', 'Blue Village 1')"; con.query(sql, function (err, result) { if (err) throw err; /*Use the result object to get the id:*/ console.log("1 record inserted, ID: " + result.insertId); }); });
C:\Users\My Name>node demo_db_insert_id.js
1 record inserted, ID: 15