Sergio Diaz Sergio Diaz - 4 years ago 198
MySQL Question

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

Answer Source

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);
   }
);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download