Home > Back-end >  Check if user already exist in MySql database in Node.js
Check if user already exist in MySql database in Node.js

Time:11-16

I am new to Node.js and SQL programming and I encountered a problem where I don't know how to get, if the user already exists in the database. I tried to check if (selectUsername.length = username) and it didn't work. I also tried with the version from a previous post in stackoverflow

 const selectUsername = conn.query("SELECT username FROM user WHERE username= "  username, function (err, row){
        if (row && row.length) {
          console.log('Case row was found!');
        } else {
            console.log('No case row was found :( !', err);
        }
      })

Username is a variable where my username from form was inserted. I always get the error Unknown column 'username' in 'where clause' I have a register form and I want to check if a user already exists so there are no multiple users in the database, so that I can show an error if a user already exists.

CodePudding user response:

Don't substitute a variable into the SQL, use a placeholder and a parameter array.

 const selectUsername = conn.query("SELECT username FROM user WHERE username= ?", [username], function (err, row){
  • Related