Jump to content

MYSQL dbPoll convert help


Taczman

Recommended Posts

Hello guys!

I have a problem with converting dbQuery result. I am trying to ask the database if a colum true or false. I can extract the value in a table with dbPoll, and it frees up the Query as well, but when I try to convert the table value to boolean value it always return nil. I have tryed dbExec, but it gives back true as long as the connection is alive. My main goal is to get a boolean value in a variable. Do you know the solution?

 

This code gives me a table

local connection = exports.dm_mysql:getConnection()

function onJoin()
    local serial = getPlayerSerial(source)
    local ipadress = getPlayerIP(source)
    local serialcheck = dbQuery(connection, "SELECT banned FROM accounts WHERE serial = ?", serial)
    local result = dbPoll(serialcheck, -1)
    outputChatBox(tostring(result), source)
end

addEventHandler("onPlayerJoin", root, onJoin)
[2021-07-26 15:22:32] [Output] : table: 04470BF8

When I try to extract tha value from the table with the default script, that is in the dbPoll description on MTA:wiki, it gives me nil, doesn't metter if the database store value of 1 ore 0.

function onJoin()
    local serial = getPlayerSerial(source)
    local ipadress = getPlayerIP(source)
    local serialcheck = dbQuery(connection, "SELECT banned FROM accounts WHERE serial = ?", serial)
    local result = dbPoll(serialcheck, -1)
    outputChatBox(tostring(result), source)
    if result then
        for _, row in ipairs ( result ) do
    
            -- by using a second loop (use it if you want to get the values of all columns the query selected):
            for column, value in pairs ( row ) do
                -- column = the mysql column of the table in the query
                -- value = the value of that column in this certain row
            end
            
            -- or without a second loop (use it if you want to handle every value in a special way):
            outputChatBox (tostring(row["column"]), source) -- it will output the value of the column "column" in this certain row
        end
    end
end

addEventHandler("onPlayerJoin", root, onJoin)
[2021-07-26 15:35:01] [Output] : nil

I had to convert the output to string, because I got warning for bad argument, that it gives me back nil value.

[2021-07-26 15:34:21] WARNING: dm_accounts\sourceS.lua:21: Bad argument @ 'outputChatBox' [Expected string at argument 1, got nil]

 

Link to comment
  • Moderators

Welcome!

local connection = exports.dm_mysql:getConnection()

function onJoin()
    local serial = getPlayerSerial(source)
    local ipadress = getPlayerIP(source)
    local serialcheck = dbQuery(connection, "SELECT banned FROM accounts WHERE serial = ?", serial)
    local result = dbPoll(serialcheck, -1)
  	
    if result and #result == 1 then -- found an account
        local banned = tonumber(result[1]["banned"]) == 1 -- A Boolean from database is 0 (false) or 1 (true), this will be true if the value 1, false otherwise 

        if banned then
            outputChatBox("Banned", source)
        else
            outputChatBox("Not banned", source)
        end
    end
end

addEventHandler("onPlayerJoin", root, onJoin)

 

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...