mysql - 如何通过escape()方法在node中使用mysql全文搜索?

标签 mysql node.js

在 Node js 中,我曾经运行 LIKE 语句查询以从 MYSQL 数据库获取数据,但是,由于性能不佳,我更新了我的查询以使用全文搜索(“where match AGAINST”语句)。我试图用“?”运行查询占位符或使用 escape() 方法(以避免 sql 注入(inject))但没有成功。查询仅在没有“?”的情况下成功运行占位符或 escape() 方法。

我已经查找了提供的其他答案,但找不到解决方案。

代码有效 - sql 注入(inject)易受攻击

function (req,res,next) {
/// req.query.Name is coming from user input of a form
    const queryString = "Select idx, descr, price, product_img,\
 stock, available from prod.product_list_details where match descr \
    against" + "(" + "'" + req.query.Name + "'" + ")"
    connection.query(queryString, (err, rows, fields) => {
      if (err) {
        console.log("Failed to query for description: " + err)
        res.sendStatus(500)
        return
      }
      console.log("I think we fetched products successfully")

代码不起作用 - 已添加?避免sql注入(inject)的占位符

function (req,res,next) {
///productDescription is from user input of a form
    var productDescription = "(" + "'" + req.query.Name+ "'" + ")" 
    const queryString = "Select idx, descr, price, product_img, stock,\
 available from prod.product_list_details where match descr against ?" 
    connection.query(queryString, productDescription, 
      (err, rows, fields) => {
      if (err) {
        console.log("Failed to query for description: " + err)
        res.sendStatus(500)
        return
      }
      console.log("I think we fetched products successfully")

我在第二次查询时收到的错误消息:

无法查询描述:错误:ER_PARSE_ERROR:您的 SQL 语法有误;检查与您的 MySQL 服务器版本对应的手册,了解在第 1 行的“(\'Clorox\')'' 附近使用的正确语法

有没有什么办法可以在node js中使用mysql全文搜索,也有办法避免sql注入(inject)?

提前感谢您的任何建议!

编辑 感谢您的回答,这对我有用。

function (req,res,next) {
    var userInput = req.query.Name ///values to search
    var modifiedUserInput = userInput.replace(/ /g,"+")
    var productDescription = "'+" + modifiedUserInput+ "'" 
    const queryString = "Select idx, descr, price, product_img, stock, available from \
    prod.product_list_details where match descr against (? IN BOOLEAN MODE)" 
    connection.query(queryString, productDescription, 
      (err, rows, fields) => {
      if (err) {
        console.log("Failed to query for description: " + err)
        res.sendStatus(500)
        return
      }
      console.log("I think we fetched products successfully")

最佳答案

当使用占位符时,请务必注意它们仅用于数据并且不能包含语法元素,这将导致解析错误。

在您的情况下,这意味着查询应采用以下形式:

WHERE MATCH (descr) AGAINST (? IN BOOLEAN MODE)

其中 ? 表示要包含的数据,并且只有数据。当你放在括号中时,它有效地扩展为:

WHERE MATCH (descr) AGAINST '(...)'

这破坏了语法,因为括号现在位于数据内部而不是包围数据。

对于 LIKE 之类的东西,其中 % 占位符是数据的一部分,您可以像这样放置它们:

WHERE x LIKE ?

在其他情况下你可以这样做:

WHERE x LIKE CONCAT('%', ?)

如果您更愿意在数据库端进行组装。

在任何情况下,使用带有占位符值的预准备语句都非常重要,因此掌握这一点非常重要,而且您正在朝这个方向努力是件好事。

希望这能帮助您到达您想去的地方。

关于mysql - 如何通过escape()方法在node中使用mysql全文搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55873685/

相关文章:

Mysql插入和删除时死锁

php - 检查查询中的值是否为 NULL

node.js - PhoneRTC:git 子模块已损坏

node.js - 来自远程 url 的 koa.js 流响应

node.js - 如何使 mongoDB 实例可用于 Node.js 中的所有其他模块

javascript - EventEmitter 没有发出

java - ResultSet 对象 JDBC 中的 Mysql 值被截断

php - Mysql - 避免Cross Join不获取多个重复数据

php - 不存在于属性中

javascript - CALL_NON_FUNCTION_AS_CONSTRUCTOR(原生)NodeJS