【问题标题】:How do I get all entries with a value in mySql then loop through foreach如何在 mySql 中获取所有具有值的条目,然后遍历 foreach
【发布时间】:2020-08-29 17:24:40
【问题描述】:

我想向不和谐 id(VERIFICATIONLINK) 发送消息,我知道如何发送带有 id 的消息,但我想知道如何循环遍历每个具有 'VERIFIED'=InProgress 的条目,获取该条目中要在 foreach 循环中使用的其他值。

图片是mysql表现在的样子enter image description here

下面的代码是我现在连接到mysql部分的代码

  host: config.mysqlhost,
  port: config.mysqlport,
  user: config.mysqluser,
  password: config.mysqlpassword,
  database: config.mysqldatabase,

})
connection.connect(err => {
  if(err) throw err;
  console.log("Connected to db")
})
client.connection = connection;

function continueVerification(){
  let Playervals = connection.query("SELECT * FROM " + config.mysqltable + " WHERE VERIFIED='InProgress'", console.log);
//take that above info and loop into foreach
//in the foreach, I will send a message to each entry via the 'LINKVERIFICATION' entry which is their discordID
}```

【问题讨论】:

    标签: mysql node.js discord discord.js


    【解决方案1】:

    你这样做:

    function continueVerification() {
       connection.query("SELECT * FROM " + config.mysqltable + " WHERE VERIFIED='InProgress'", (err, rows) => {
            rows.forEach(row => {
                /// our code
            })
        })
    }
    

    或者如果你想要 mysql 选择结果的 const 值,你需要使用promise,因为mysql.query 是一个callback 函数。

    const Playervals = await getVerifiedUsers()
    
    const getVerifiedUsers = () => {
        return new Promise(resolve => {
            connection.query("SELECT * FROM " + config.mysqltable + " WHERE VERIFIED='InProgress'", (err, rows) => {
                resolve(rows)
            })
        })
    }
    

    【讨论】:

    • 在 foreach 循环中,如何将值 'LinkedID' 作为 const 获取?
    • 我知道在 java 中我可以使用 results.getString("LINKEDID")
    • linkedid 是什么?
    猜你喜欢
    • 2018-01-02
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 2012-05-17
    相关资源
    最近更新 更多