【问题标题】:getting mysql syntax error while using parameters in mysql query在mysql查询中使用参数时出现mysql语法错误
【发布时间】:2022-01-20 03:51:19
【问题描述】:

nodejs中出现sql语法错误

我正在使用此代码:

async function getNotificationFsql(data) {
    var query = "select * from notifications where id>${data.start} and receiver=${data.recId} limit 10";
    var record = await db.sequelize.query(query ,
        function (err,results) {
            if (err) {
                console.log('Error while getting data from MYSQL     :-   ' + err);
            } else {
                console.log('Notifications data which we got from MYSQL     :-   ' + results);
            }
        }
    );
    // console.log(record);
    console.log('Notifications      :-   ' + record[0]);
    return record[0];
};

我得到了:

代码:'ER_PARSE_ERROR', 错误号:1064, sqlState: '42000',

sqlMessage:“您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,了解在 '{data.start} 和 receiver=${data.recId} limit 10' 附近使用的正确语法在第 1 行",

sql: 'select * from notifications where id>${data.start} and receiver=${data.recId} limit 10', 参数:未定义 }, sql: 'select * from notifications where id>${data.start} and receiver=${data.recId} limit 10', 参数:未定义

我做错了什么?

【问题讨论】:

  • 你没有用明确的值替换占位符。
  • 不要使用字符串替换构建查询,使用参数化查询。

标签: javascript mysql node.js macos


【解决方案1】:

对不起,我做错了

我这样做解决了这个错误:

var query = "select * from notifications where id>"+data.start+" and receiver="+data.recId+" limit 10";

【讨论】:

  • 此代码可免费用于 SQL 注入...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-14
相关资源
最近更新 更多