0

Whats wrong with my query? im having this error: enter image description here

con.query(
           'SELECT nick FROM channels WHERE room=1room',
           function(err, rows) {
               if (err) throw err;
               console.log(rows);
           }
       );

I tried this, and i have the same error:

var room = "1room";
       con.query(
           'SELECT nick FROM channels WHERE room=' + room,
           function(err, rows) {
               if (err) throw err;
               console.log(rows);
           }
       );

1 Answer 1

2

It is treating 1room as a variable, not a string. Wrap it in quotes and it should work.

con.query(
   'SELECT nick FROM channels WHERE room="1room"',
   function(err, rows) {
       if (err) throw err;
       console.log(rows);
   }
);

For your second example, you should get in the habit of escaping the variables you use in queries for security reasons (to prevent SQL injection).

var room = "1room";
con.query(
   'SELECT nick FROM channels WHERE room=?',
   [room],
   function(err, rows) {
       if (err) throw err;
       console.log(rows);
   }
);
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.