=============================================== ==
let x = tempCont.query('SELECT password FROM `users` WHERE `user_name` =' + req.body.user_name,
function (err, result, field) {
if (!!error) {
console.log('Error in user_name Query');
}
else {
console.log(result);
}
console.log(x[0]);
})
答案 0 :(得分:1)
您应该在查询中使用参数,即
let x = tempCont.query('SELECT password FROM `users` WHERE `user_name` = ?',
[req.body.user_name],
function (err, result) {
if (err) {
console.log('Error in user_name Query');
}
else {
console.log(result);
}
});
通过将username变量作为参数传递给数组,应该已经为你完成了正确的转义。
答案 1 :(得分:0)
怎么样
var x = 0;
tempCont.query('SELECT password FROM `users` WHERE `user_name` =' + req.body.user_name,
function (err, result, field) {
if (!!error) {
console.log('Error in user_name Query');
}
else {
console.log(result);
}
x = result
// to display password of row 1
console.log(x[0].password);
// to display row 1
console.log(x[0]);
// to display the object
console.log(x);
})
也可以使用以下格式:
tempCont.query('SELECT password FROM `users` WHERE `user_name` = ?',[req.body.user_name],(err, result, field){
});
答案 2 :(得分:0)
以下是我如何使用查询数据并在外部使用它。像外部模块一样使用它。
{{1}}
在这种情况下,将结果对象分配给req.user,但此示例使用express