mysqloo problem

I have a function:


function getinfo(ply, column)
local q = db:query("SELECT * FROM `ttt` WHERE `uniqueid`='"..ply:UniqueID().."'")
	
	function q:onSuccess(data)
	local points
	if #data > 0 then
	points = tonumber(data[1][column])
    else
	points = 0
	end
	end

	function q:onError(err, sql)
		MsgN('Query Failed: ' .. err .. '(' .. sql .. ')')
	end
	q:start()
    return points	
	end

This function returns nil value. I’m absolutelly sure that table name and column name are right. What’s wrong in this function? Thanks.

You have to return ‘points’ in your onSuccess callback.


function getinfo(ply, column)
    local q = db:query("SELECT * FROM `ttt` WHERE `uniqueid`='"..ply:UniqueID().."'")
        
    function q:onSuccess(data)
        local points
        if #data > 0 then
            points = tonumber(data[1][column])
        else
            points = 0
        end
        return points
    end

    function q:onError(err, sql)
        MsgN('Query Failed: ' .. err .. '(' .. sql .. ')')
    end
    q:start()    
end

Code for function call:
local player_points = tonumber(getinfo(ply, “points”))

Still doesn’t work. Error “Bad argument #1 to ‘tonumber’ (value expected)”.

You can’t call your function like that because your query doesn’t return instantly, therefore tonumber() won’t have anything to work with.

Either you add q:wait(), which stops the server until the query is completed (and possibly crash it) or you restructure your function.

q:wait() Variant:


function getinfo(ply, column)
    local q = db:query("SELECT * FROM `ttt` WHERE `uniqueid`='"..ply:UniqueID().."'")
        
    function q:onSuccess(data)
        local points
        if #data > 0 then
            points = tonumber(data[1][column])
        else
            points = 0
        end
        return points
    end

    function q:onError(err, sql)
        MsgN('Query Failed: ' .. err .. '(' .. sql .. ')')
    end
    q:start()
	q:wait()
end

Passing a function for the callback variant:


function getinfo(ply, column, func)
    local q = db:query("SELECT * FROM `ttt` WHERE `uniqueid`='"..ply:UniqueID().."'")
        
    function q:onSuccess(data)
        local points
        if #data > 0 then
            points = tonumber(data[1][column])
        else
            points = 0
        end
        func(points)
    end

    function q:onError(err, sql)
        MsgN('Query Failed: ' .. err .. '(' .. sql .. ')')
    end
    q:start()
end


getinfo(ply, "points", function(points)
	local player_points = tonumber(points)
	
	
	<continue code here>
	
	
end)