• mysqloo problem
    3 replies, posted
I have a function: [CODE]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[/CODE] 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. [CODE]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]
[QUOTE=Sgt.Hunter;41199601]You have to return 'points' in your onSuccess callback. [CODE]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][/QUOTE] 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: [CODE]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[/CODE] Passing a function for the callback variant: [CODE]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[/CODE] [CODE]getinfo(ply, "points", function(points) local player_points = tonumber(points) <continue code here> end)[/CODE]
Sorry, you need to Log In to post a reply to this thread.