Sergio Diaz
Sergio Diaz

Reputation: 33

Query not working using node.js node-mysql

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);
           }
       );

Upvotes: 0

Views: 1355

Answers (1)

displaynamevalue
displaynamevalue

Reputation: 138

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);
   }
);

Upvotes: 2

Related Questions