0

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.

7
  • 1
    You're missing quotes around the username so you're almost certainly getting an error. Commented Nov 16, 2021 at 9:01
  • 1
    Don't concatenate variables into the SQL. Use placeholders and a parameter array. Commented Nov 16, 2021 at 9:01
  • Why aren't you checking err? Commented Nov 16, 2021 at 9:02
  • Assigning the username to the length (?) of another username doesn't sound like a useful operation to. You need to use placeholders as Barmar suggested. Commented Nov 16, 2021 at 9:03
  • @Barmar i am checkig the err i just forgot to put it in the post Commented Nov 16, 2021 at 9:04

1 Answer 1

0

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){
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.