我正在尝试创建一个自动完成搜索使用快速/书架/Awesomeplete。
我被困在试图找出传递的搜索词到书架使用像这样的Where Like查询,我正在从PHP/MYSQL转换。
$query = mysql_escape_string($_REQUEST['query']);
$query = htmlentities($query);
SELECT SID, schoolName FROM schools WHERE schoolName LIKE '%$query%'这是我到目前为止所拥有的。
var terms = req.params.search;
new Model.School()
//This is the part that I can't get to work.
.query('where', 'schoolName', 'LIKE', '%'?'%',[terms])
.fetchAll({columns: ['SID', 'schoolName']})
.then(function (schools) {
console.log(schools);
var schools = schools.toJSON();
res.json(schools);
})
.catch(function (error) {
res.json({'message':'An error occured in your search'});
});如果我将上面的内容更改为:
new Model.School()
.query('where', 'schoolName', 'LIKE', '%american%')
.fetchAll({columns: ['SID', 'schoolName']})
.then(function (schools) {
console.log(schools);
var schools = schools.toJSON();
res.json(schools);
})
.catch(function (error) {
res.json({'message':'An error occured in your search'});
});书架查询按我需要的方式工作,但我希望查询参数是动态的。我已经尝试了一大堆排列,但不能让它工作。
发布于 2018-01-15 10:52:03
从我的评论和你的验证来看,下面的方法看起来是可行的。
基本上,在将'%‘传递到.query LIKE子句之前,您希望将’%‘附加到搜索值。
var terms = "%"+req.params.search+"%";
new Model.School()
.query('where', 'schoolName', 'LIKE', terms)
.fetchAll({columns: ['SID', 'schoolName']})
.then(function (schools) {
console.log(schools);
var schools = schools.toJSON();
res.json(schools);
})
.catch(function (error) {
res.json({'message':'An error occured in your search'});
});祝你编码愉快。加里。
https://stackoverflow.com/questions/48234836
复制相似问题