I have a MySQL database and I'm working with NodeJS to getting access to it. Here is the code that I'm using to make an INSERT in my table.
var mysql = require('mysql');
var fs = require('fs');
var readline = require('readline');
var ID_User = {value: 0};
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "blah",
multipleStatements: true
});
function insertNewUser(name_user, password_user) {
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql_user = "INSERT INTO `blah`.`User` (`Name`, `Password`) VALUES ('" + name_user + "', '" + password_user + "');"
con.query(sql_user, function (err, result) {
if (err) throw err;
ID_User.value = result.insertId;
console.log(ID_User);
});
});
}
And this is the code that I'm using to execute this function:
console.log("user " + ID_User.value + " " + ID_User);
insertNewUser("user", "123");
console.log("some message");
console.log("user " + ID_User.value + " " + ID_User);
Everything is in the same file, but when I execute the code, this is my output:
Why it happens this? How I can control the order of execution of this functions? I tried to put make a second function but this one (insertNewUser()) always is executed until the end.
Thanks beforehand.