有人知道如何在node-mysql中使用SELECT WHERE IN吗?
我已经尝试了下面的代码,但我得到了以下错误消息:
'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''(`PHP`,`apache`)'' at line 1'这是我的代码:
whereIn = '(';
for ( var i in tagArray ) {
if ( i != tagArray.length - 1 ) {
whereIn += "`" + tagArray[i] + "`,";
}else{
whereIn += "`" + tagArray[i] + "`";
}
}
whereIn += ')';
console.log(whereIn);
client.query(
'SELECT tag_id FROM tag WHERE tag_name IN ?',
[whereIn],
function(err, result, fields) {
client.destroy();
if (err) {
throw err;
}
console.log(result);
res.redirect('/');
}
);发布于 2015-03-06 17:21:36
您必须使用IN (?)而不是IN ?。
任何字符串操作都可能导致SQL注入后门。
发布于 2018-07-30 21:03:06
您只需要将值的tagArray传递给node-mysql,它将为您处理其余的工作:
client.query(
'SELECT tag_id FROM tag WHERE tag_name IN (?)',
[tagArray],
function(err, result, fields) {
client.destroy();
if (err) {
throw err;
}
console.log(result);
res.redirect('/');
}
);有关更多信息,请参阅手册中有关如何自动转义不同值的部分:https://github.com/mysqljs/mysql#escaping-query-values
发布于 2012-06-15 05:02:12
你需要引用你的字符串,而不是使用反引号。
whereIn = '(';
for ( var i in tagArray ) {
if ( i != tagArray.length - 1 ) {
whereIn += "'" + tagArray[i] + "',";
}else{
whereIn += "'" + tagArray[i] + "'";
}
}
whereIn += ')';https://stackoverflow.com/questions/11041231
复制相似问题