简体   繁体   English

Discord.js 中的节点 mysql 返回空结果/数组

[英]Node mysql in Discord.js returns empty result/array

I'm trying to make a command that get the selected queries from a table where the id is the one i use in the command, for example: !db 1 but I'm having a problem.我正在尝试创建一个命令,从表中获取所选查询,其中 id 是我在命令中使用的表,例如: !db 1但我遇到了问题。 The problem is that the result is empty.问题是结果是空的。

My code:我的代码:

const Discord = require('discord.js');
const mysql = require('mysql');

module.exports.run = async (bot, message, args, connection) => {
    const asd = args.slice(1,2).join(' ');

    let querystring = `SELECT * FROM test WHERE id = '${asd}'`

    connection.query(querystring, function (err, results, rows) {
    if (err) throw err;

    console.log(results);
    });

}

module.exports.help = {
    name: "db"
}

I appreciate any help!我很感激任何帮助! Thanks!谢谢!

From the screenshot you posted earlier , your id column is a type INT .从您之前发布的屏幕截图中,您的id列是一个INT类型。 This code is searching as if the column is a VARCHAR .此代码正在搜索,好像该列是VARCHAR

Try this:尝试这个:

const id = args.slice(1, 2).join(' ');
if (isNaN(id)) { return; } // if the input isn't a number
connection.query(`SELECT * FROM test WHERE id = ${Number.parseInt(id)}`, (err, res, rows) => {
    if (err) throw new Error(err);
    console.log(res);
});

Important : This code allows SQL Injection .重要提示:此代码允许SQL 注入 Template literals do not protect against this.模板文字不能防止这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM