• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

How to put multiples values from database in one variable?

Joined
Mar 14, 2020
Messages
139
Solutions
3
Reaction score
11
I need to get all Tibia Coins that we have in the server and save in one variable, how i do that?

I tryed:

Lua:
local allCoins = db.storeQuery("SELECT `coins` FROM `accounts` LIMIT 0;")
doPlayerPopupFYI(cid, "total coins: ".. allCoins.."")
Got this error:

1607353265849.png

Also tryed:

Code:
function onSay(cid, words, param)
    local allCoins = db.storeQuery("SELECT `coins` FROM `accounts` LIMIT 9999;")
    local list = {}
    if allCoins:getID() == -1 then
        return true
    end
    repeat
        local coins = allCoins:getDataString("coins")
        table.insert(list, coins)
    until not allCoins:next()
    doPlayerPopupFYI(cid, "total coins: ".. table.concat(list,"\n") .."")
end

Got this error. Why god is so hard to get a simple data information from database in lua?

1607353357015.png
 

Attachments

Solution
Faster.
Lua:
function onSay(cid, words, param)
    if getPlayerGroupId(cid) ~= 6 then
        return true
    end
    local sumCoins = 0
    local allCoins = db.storeQuery("SELECT SUM(`coins`) AS `coins` FROM `accounts`")
    if allCoins ~= false then
        sumCoins = result.getNumber(allCoins, 'coins')
        result.free(allCoins)
    end
    doPlayerPopupFYI(cid, "Total Coins: ".. sumCoins.. "")
    return true
end
Code:
SELECT SUM(`coins`) FROM `accounts`;

hints:
local allCoins = db.storeQuery("SELECT SUM(coins) FROM accounts LIMIT 0;") , still same boolean error
local allCoins = db.storeQuery("SELECT SUM(coins) FROM accounts;") , returns a number that increase with usage(?) like image below.
1607397324657.png
Post automatically merged:

Solved using this code:

Lua:
function onSay(cid, words, param)
    if getPlayerGroupId(cid) ~= 6 then
        return true
    end
    local sumCoins = 0
    local allCoins = db.storeQuery("SELECT `coins` FROM `accounts`")
    if allCoins ~= false then
        repeat
            local task1 = result.getString(allCoins, 'coins')
            sumCoins = tonumber(task1)+sumCoins
        until not result.next(allCoins)  
        result.free(allCoins)
    end
    doPlayerPopupFYI(cid, "Total Coins: ".. sumCoins.. "")
return true
end
 
Last edited:
Faster.
Lua:
function onSay(cid, words, param)
    if getPlayerGroupId(cid) ~= 6 then
        return true
    end
    local sumCoins = 0
    local allCoins = db.storeQuery("SELECT SUM(`coins`) AS `coins` FROM `accounts`")
    if allCoins ~= false then
        sumCoins = result.getNumber(allCoins, 'coins')
        result.free(allCoins)
    end
    doPlayerPopupFYI(cid, "Total Coins: ".. sumCoins.. "")
    return true
end
 
Solution
Faster.
Lua:
function onSay(cid, words, param)
    if getPlayerGroupId(cid) ~= 6 then
        return true
    end
    local sumCoins = 0
    local allCoins = db.storeQuery("SELECT SUM(`coins`) AS `coins` FROM `accounts`")
    if allCoins ~= false then
        sumCoins = result.getNumber(allCoins, 'coins')
        result.free(allCoins)
    end
    doPlayerPopupFYI(cid, "Total Coins: ".. sumCoins.. "")
    return true
end
better, ty
 
Back
Top