Search code examples
javascriptjqueryreturn

How can I put the row number of a database in a variable


there is my problem :

{
const row_nb = con.query("SELECT COUNT(*) FROM Users WHERE username = '"+username_ins+"'");
console.log(""+row_nb);
}

but I dont have the row number why ? it return [Object object] And it's in javaScript


Solution

  • con.query is an async method you have to pass the callback to get the result.

    var sql = "SELECT COUNT(*) as usersCount FROM Users WHERE username = ?"
    con.query(sql, [username_ins], function(err, rows, fields) {
      if (err) throw err;
      console.log('Query result: ', rows[0].usersCount);
    });