使用参数查询连接对象

当你想在 SQL 中使用用户生成的内容时,它完成了参数。例如,对于使用名称 aminadav 搜索用户,你应该:

var username = 'aminadav';
var querystring = 'SELECT name, email from users where name = ?'; 
connection.query(querystring, [username], function(err, rows, fields) {
  if (err) throw err;
  if (rows.length) {
    rows.forEach(function(row) {
      console.log(row.name, 'email address is', row.email);
    });
  } else {
    console.log('There were no results.');
  }
});