Retrieve last inserted id with Mysql

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);
    }
});

You can also view by visiting this link https://github.com/mysqljs/mysql#getting-the-id-of-an-inserted-row


https://github.com/mysqljs/mysql#getting-the-id-of-an-inserted-row describes the solution perfectly well:

connection.query('INSERT INTO posts SET ?', {title: 'test'}, function(err, result, fields) {
  if (err) throw err;

  console.log(result.insertId);
});

Tags:

Mysql

Node.Js