gpt4 book ai didi

javascript - node.js mysql LIKE语句用法

转载 作者:太空宇宙 更新时间:2023-11-03 11:39:08 26 4
gpt4 key购买 nike

app.post('/like/:level/:name', function(req, res){
connection.query("SELECT * from books where " + req.params.level + " like '%" + req.params.name + "'%", function(err, rows, fields) {
if (!err){
var row = rows;
res.send(row);
console.log(req.params);
console.log('The solution is: ', rows);}
else{
console.log('Error while performing Query.');
console.log(err);}
});
});

根据上面的代码,谁能帮我找到无法使用 LIKE 语句发起查询的原因?

the error is shown as
{ Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%' at line 1
at Query.Sequence._packetToError (/root/Newfolder/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
at Query.ErrorPacket (/root/Newfolder/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
at Protocol._parsePacket (/root/Newfolder/node_modules/mysql/lib/protocol/Protocol.js:280:23)
at Parser.write (/root/Newfolder/node_modules/mysql/lib/protocol/Parser.js:75:12)
at Protocol.write (/root/Newfolder/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/root/Newfolder/node_modules/mysql/lib/Connection.js:103:28)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
--------------------
at Protocol._enqueue (/root/Newfolder/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Connection.query (/root/Newfolder/node_modules/mysql/lib/Connection.js:208:25)
at /root/Newfolder/trial.js:98:12
at Layer.handle [as handle_request] (/root/Newfolder/node_modules/express/lib/router/layer.js:95:5)
at next (/root/Newfolder/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/root/Newfolder/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/root/Newfolder/node_modules/express/lib/router/layer.js:95:5)
at /root/Newfolder/node_modules/express/lib/router/index.js:281:22
at param (/root/Newfolder/node_modules/express/lib/router/index.js:354:14)
at param (/root/Newfolder/node_modules/express/lib/router/index.js:365:14)
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
index: 0 }

最佳答案

您的查询字符串中的单引号位置错误。改变这个:

" like '%" + req.params.name + "'%"

...为此:

" like '%" + req.params.name + "%'"

...所以第二个百分号在单引号内。

如果您想按照评论中提到的那样进行“开头为”搜索,请从字段值的开头删除 '%':

" like '" + req.params.name + "'%"

最后,不是你问的,而是你 shouldn't directly concatenate user input into an SQL query .

关于javascript - node.js mysql LIKE语句用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43359816/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com