MySql always returning nil when data is in the database

Hi, I am trying to trigger a callback to the client to display some data but when I trigger the callback the my sql query return nil.
here is the errors u can see the data is nil by my prints in the script


SQL
Screenshot_6

The script should return a Callback of 0 to the client script as 0 is in the sql but it doesnt its nil even with a number there its still nil

Here is the code:

server

QBCore.Functions.CreateCallback('atmrob:timeout', function(source, cb)
	local _source = source
	local xPlayer = QBCore.Functions.GetPlayer(_source)
	local id = xPlayer.PlayerData.citizenid
	print(id)
    --cb(getPet(xPlayer.identifier))
	
	local result = MySQL.query.await('SELECT atmtimeout FROM players WHERE citizenid = ?', { id })
	if result[1] then
		print(result[1].atmtimeout)
		cb(result[1].atmtimeout)
	else
		cb(0)
	end
end)

CLient

QBCore.Functions.TriggerCallback("atmrob:timeout", function(data)
            Timeout = data
            print(data)
            if Timeout == nil then
                TriggerServerEvent("atmrob:update", 0)
                Timeout = 0
                print("it was nil ffs")
            end

            
            --print(Timeout)
            RegisterCommand("atmtimeout", function()
                exports['okokNotify']:Alert("ATM Robbery", "You have "..tostring(math.floor(Timeout/60000)).." Mins before you can rob an ATM", 7000, 'info')
            end,false)
                
            while Timeout > 0 do
                local xPlayer = QBCore.Functions.GetPlayerData()
                Timeout = Timeout - Config.SqlUpdateTime
                --print(Timeout)
                TriggerServerEvent("atmrob:update", Timeout)
                    
                Wait(Config.SqlUpdateTime * 1000)
                if Timeout == 0 then
                    Robbing = false 
                    TriggerServerEvent("atmrob:update", Timeout)
                end
            end
        end) 

Have you printed result[1] what is that. I don’t know that exact MySQL query syntax as I personally use execute, fetchAll, and fetchScalar, but is your query calling a fetchScalar as there is only one column to collect?

MySQL.query.await always returns a table - even if there are no results. I don’t see why your query isn’t working.

Try using MySQL.single.await if you only need a single row:

local result = MySQL.single.await(SELECT atmtimeout FROM players WHERE citizenid = ?', { id })
if result then
    print(result.atmtimeout)
end

or MySQL.scalar.await if you only need a value from a single row:

local atmtimeout = MySQL.scalar.await(SELECT atmtimeout FROM players WHERE citizenid = ?', { id })
print(atmtimeout)
1 Like

Thank you for your help!

1 Like