NodeJS代码如下:
app.get('/search', function(req, res){
var keyword = req.query.q;
con.query("SELECT Post_Title, Post_Icon, Post_Cont, Post_Author, Post_Date FROM Posts WHERE Post_Title LIKE '" + keyword + "' OR Post_Icon LIKE '" + keyword + "' OR Post_Cont LIKE '" + keyword + "' OR Post_Author LIKE '" + keyword + "' OR Post_Date LIKE '" + keyword + "' ORDER BY Post_Date ASC", function (err, result) {
if (err){
console.log("Error on DB SELECT.");
console.log(err);
tellSelectError(req, res);
}else{
console.log("Database selected");
console.log(result);
/*res.render('index', {
info: info,
result: result
});*/
res.json(result);
}
});
});
它将空json发送到客户端浏览器。
屏幕截图上传于:https://i.stack.imgur.com/kpSDA.jpg
请帮助.....
这段代码有效:
SELECT * FROM Posts WHERE Post_ID = " + keyword
但我希望 LIKE 使用帖子的所有颜色,不包括 Post_ID 。
的console.log(ERR);记录没有错误。
得到消息:
当我将SQL更改为SELECT * FROM Posts
时,它正确返回所有原始数据,但SELECT Post_Title, Post_Icon, Post_Cont, Post_Author, Post_Date FROM Posts WHERE Post_Title LIKE '" + keyword + "' OR Post_Icon LIKE '" + keyword + "' OR Post_Cont LIKE '" + keyword + "' OR Post_Author LIKE '" + keyword + "' OR Post_Date LIKE '" + keyword + "' ORDER BY Post_Date ASC
未按预期工作。
答案 0 :(得分:0)
您需要将传递给查询的值包装在引号中。所以正确的语法应该是:
"SELECT Post_Title, Post_Icon, Post_Cont, Post_Author, Post_Date
FROM Posts
WHERE Post_Title LIKE '" + keyword + "' OR Post_Icon LIKE '" + keyword + "' OR Post_Cont LIKE '" + keyword + "' OR Post_Author LIKE '" + keyword + "' OR Post_Date LIKE '" + keyword + "' ORDER BY Post_Date ASC"
注意:LIKE是一个运算符,用于代替=来搜索字段内的值。 =将尝试匹配整个字段。为此,LIKE在三个不同的选项中使用通配符(%):
如果您不使用通配符,则使用LIKE
是没用的