Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a code that pulls only 1 result and I need it to pull all results (or limit it to 10). Can someone help me out please?


JavaScript
con.query(`SELECT * FROM events WHERE closed = 'No'`, (err, rows) => {
        if (err) throw err;

        let sql;
        let eventID = rows[0].eventnumber;
        let hostID = rows[0].host;
        let description = rows[0].eventname;
        let participants = rows[0].participants

        const event = `Event ${eventID}: ${description} - Hosted by: <@` + hostID + `>. Participants: ${participants}`

        const listEmbed = new Discord.MessageEmbed()
            .setColor('#ff6600')
            .setTitle('Events')
            .setDescription(`${event}`)


        message.channel.send(listEmbed);

        con.query(sql);
    });

The information from the database is as below:

eventnumber	host	            eventname	closed	participants
16	        123456789012345678	test raid	Yes	    test0 test1
17	        123456789012345678	test raid	No	    test2 test3 test4
18	        123456789012345678	test raid	No	    test5 test6 test7
19	        123456789012345678	test raid	Yes	    test8 test9 test10


What I have tried:

I've tried looking for similar topics on this and the closest I could get was this below but it kept throwing an error because of the .all()

JavaScript
const list = sql.prepare("SELECT * FROM events WHERE closed = 'No' ORDER BY eventnumber DESC LIMIT 10;").all();

  const embed = new Discord.MessageEmbed()
    .setTitle("Events")
    .setDescription("Next 10 events")
    .setColor(0x00AE86);

  for(const data of list) {
    embed.addFields({ name: "Event #", value: `${data.eventnumber}`});
    embed.addFields({ name: "Hosted by:", value: `<@` + data.host + `>`});
    embed.addFields({ name: "Event Description", value: `${data.eventname}`});
    embed.addFields({ name: "Participants", value: `${data.participants}`});
  }
  return message.channel.send({embed});
}


EDIT:

So I found a way to make it show 10 events but it looks ugly and I know there is a better way just can't seem to figure it out. Also if there are not 10 results that meet the condition, the code errors out.

JavaScript
con.query(`SELECT * FROM events WHERE closed = 'No'`, (err, rows) => {
        if (err) throw err;

        let sql;
        let firstID = rows[0].eventnumber;
        let firstHost = rows[0].host;
        let firstDescription = rows[0].eventname;
        let firstParticipants = rows[0].participants;
        let secondID = rows[1].eventnumber;
        let secondHost = rows[1].host;
        let secondDescription = rows[1].eventname;
        let secondParticipants = rows[1].participants;
        let thirdID = rows[2].eventnumber;
        let thirdHost = rows[2].host;
        let thirdDescription = rows[2].eventname;
        let thirdParticipants = rows[2].participants;
        let fourthID = rows[3].eventnumber;
        let fourthHost = rows[3].host;
        let fourthDescription = rows[3].eventname;
        let fourthParticipants = rows[3].participants;
        let fifthID = rows[4].eventnumber;
        let fifthHost = rows[4].host;
        let fifthDescription = rows[4].eventname;
        let fifthParticipants = rows[4].participants;
        let sixthID = rows[5].eventnumber;
        let sixthHost = rows[5].host;
        let sixthDescription = rows[5].eventname;
        let sixthParticipants = rows[5].participants;
        let seventhID = rows[6].eventnumber;
        let seventhHost = rows[6].host;
        let seventhDescription = rows[6].eventname;
        let seventhParticipants = rows[6].participants;
        let eigthID = rows[7].eventnumber;
        let eigthHost = rows[7].host;
        let eigthDescription = rows[7].eventname;
        let eigthParticipants = rows[7].participants;
        let ninethID = rows[8].eventnumber;
        let ninethHost = rows[8].host;
        let ninethDescription = rows[8].eventname;
        let ninethParticipants = rows[8].participants;
        let tenthID = rows[9].eventnumber;
        let tenthHost = rows[9].host;
        let tenthDescription = rows[9].eventname;
        let tenthParticipants = rows[9].participants;

        const firstEvent = `**Event ${firstID}:** ${firstDescription}\n**Hosted by:** <@` + firstHost + `>\n**Participants:** ${firstParticipants}`
        const secondEvent = `**Event ${secondID}:** ${secondDescription}\n**Hosted by:** <@` + secondHost + `>\n**Participants:** ${secondParticipants}`
        const thirdEvent = `**Event ${thirdID}:** ${thirdDescription}\n**Hosted by:** <@` + thirdHost + `>\n**Participants:** ${thirdParticipants}`
        const fourthEvent = `**Event ${fourthID}:** ${fourthDescription}\n**Hosted by:** <@` + fourthHost + `>\n**Participants:** ${fourthParticipants}`
        const fifthEvent = `**Event ${fifthID}:** ${fifthDescription}\n**Hosted by:** <@` + fifthHost + `>\n**Participants:** ${fifthParticipants}`
        const sixthEvent = `**Event ${sixthID}:** ${sixthDescription}\n**Hosted by:** <@` + sixthHost + `>\n**Participants:** ${sixthParticipants}`
        const seventhEvent = `**Event ${seventhID}:** ${seventhDescription}\n**Hosted by:** <@` + seventhHost + `>\n**Participants:** ${seventhParticipants}`
        const eigthEvent = `**Event ${eigthID}:** ${eigthDescription}\n**Hosted by:** <@` + eigthHost + `>\n**Participants:** ${eigthParticipants}`
        const ninethEvent = `**Event ${ninethID}:** ${ninethDescription}\n**Hosted by:** <@` + ninethHost + `>\n**Participants:** ${ninethParticipants}`
        const tenthEvent = `**Event ${tenthID}:** ${tenthDescription}\n**Hosted by:** <@` + tenthHost + `>\n**Participants:** ${tenthParticipants}`

        const listEmbed = new Discord.MessageEmbed()
            .setColor('#ff6600')
            .setTitle('Events')
            .setDescription(`${firstEvent}\n\n${secondEvent}\n\n${thirdEvent}\n\n${fourthEvent}\n\n${fifthEvent}\n\n${sixthEvent}\n\n${seventhEvent}\n\n${eigthEvent}\n\n${ninethEvent}\n\n${tenthEvent}`)


        message.channel.send(listEmbed);

        con.query(sql);
    });


I really would appreciate the help.

Thanks.
Posted
Updated 28-Oct-21 11:09am
v2
Comments
Richard MacCutchan 24-Apr-21 3:59am    
You need to learn how to write a loop.

Needed to add a .forEach so that it will post once for each event. Flood protection may be needed.

JavaScript
con.query(`SELECT * FROM events WHERE closed = 'No'`, (err, rows) => {
        if (err) throw err;

        rows.forEach(row => {
            const eventID = row.eventnumber;
            const eventHost = row.host;
            const eventDescription = row.eventname;
            const eventParticipants = row.participants

            const event = `**Event ${eventID}:** ${eventDescription}\n**Hosted by:** <@` + eventHost + `>\n**Participants:** ${eventParticipants}`

            const listEmbed = new Discord.MessageEmbed()
                .setColor('#ff6600')
                .setTitle('Events')
                .setDescription(`${event}`)

            message.channel.send(listEmbed);
        });
    });
 
Share this answer
 
@xKaspr


Thank you soooo much for this example, it helped me fix my issue <3
 
Share this answer
 
Comments
CHill60 29-Oct-21 4:18am    
If you want to comment on a solution then use the "Have a Question or Comment?" link next to it. Don't post questions or comments as a "solution"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900