我试图使用res.end显示查询结果,但我不能,因为我是Node.js的新手,这是我的代码的一部分,其中用户在html文件中输入表单中的用户名和密码,我想显示查询结果,即用户名,但我得到的是数据库表中的所有行:
app.post('/login/:email', function (req, res){
connection.connect();
email = req.body.email;
password = req.body.password;
if(email && password )
{
console.log(email);
console.log(password);
connection.query('SELECT username FROM user WHERE email = email and password = password', function (error, rows, fields) {
str='';
for(i=0;i<rows.length;i++)
str = str + rows[i].username +'\n';
res.end( str);
});
connection.end();
}
});
答案 0 :(得分:0)
因为这个原因你得到了全桌:
connection.query('SELECT username FROM user WHERE email = email and password = password', function (error, rows, fields) {
每一行都有email = email and password = password
,这是因为左侧和右侧都被评估为列名。这就像说SELECT * FROM TABLE WHERE 1 = 1
。
尝试将其更改为:
connection.query('SELECT username FROM user WHERE email = ? and password = ?', [email, password], function (error, rows, fields) {
答案 1 :(得分:0)
connection.query('SELECT username FROM user WHERE email =“email”and password =“password”',function(error,rows,fields)
//希望这会有效我添加“”因为字符串在查询中包含“”