M
M
madhowrad2021-07-25 23:32:31
MySQL
madhowrad, 2021-07-25 23:32:31

How to display data from msql?

I have products in the database, but somehow I need to display them, I could only output them individually, help plz.

if(text === 'Магазин'){
        man = connection.query('SELECT * FROM `products`', function(error,results){
            if(error) {throw error}
            const name = results[1]['name'];
            const price = results[1]['price'];
            bot.sendMessage(chatId, `Продукты: \nИмя: ` + name + ` Цена: ` + price);
            bot.sendMessage(chatId, results[1]['name']);
        });
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Rozhnev, 2021-07-26
@madhowrad

We use a loop:

if(text === 'Магазин'){
        man = connection.query('SELECT * FROM `products`', function(error,results){
            if(error) {throw error}
            for (let i in results) {
                bot.sendMessage(chatId, `Продукты: \nИмя: ${results[i]['name']} Цена: ${results[i]['price']}`);
                bot.sendMessage(chatId, results[i]['name']);
            }
        });
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question