Using MySQLOO to return int?

[lua]
function meta:CheckMoney()
local moneyquery = databaseObject:query(“SELECT rp_money FROM loop_playerinfo WHERE rp_unique_id = '” … self:SteamID() … “’”)

moneyquery.onSuccess = function(q, data)
    local money = moneyquery:getData()

money = money[1]
//PrintTable(data)
return data
//return stringOut

end
moneyquery.onError = function(q,e) print("something went wrong when getting money") end
moneyquery:start()

end
[/lua]

I’m able to print money.rp_money correctly form inside the function, but once we return the value in that function, and try using CheckMoney in another function it gives us a nil value.

Shouldn’t you be returning money, not data?

Money is a table, they’re both tables and we run into the same problem

money = money[1] that isn’t a table, thats the first row of the table (which is data). Im not very experienced with LUA but try
money = tonumber(money[1])

Why run a query every time when you can just store the data when you load their profile then have functions to handle the variable. You would then update the money on the database whenever you need to save it (disconnect and/or an interval)

[editline]21st December 2012[/editline]

You also can’t return a value since the callback has a delay and it is out of the scope.

Oops wrong place… Delete this? <.<

There is a way to return a value that requires the main thread to wait for the query to finish (very, very bad practice) but it’ll go something like this:

[lua]function meta:CheckMoney()
local query = databaseObject:query(“SELECT rp_money FROM loop_playerinfo WHERE rp_unique_id = '” … self:SteamID() … “’”)

local retval = 0

query.onSuccess = function(q, data)
	local money = data[1]
	retval = money -- retval is in the scope of this function
end

query.onError = function(q, e)
	print("MySQL Error: " .. e)
	retval = nil
end

query:start()
query:wait() -- This forces the main thread to wait until the query finishes

return retval -- which allows the CheckMoney function to return the return value of the query

end

– usage
local money = pl:CheckMoney()
if (money == nil) then – notice query.onError sets the return value to nil so you’ll know something went wrong
– do error handle stuff here idk
else
pl.Money = money
end[/lua]

Untested.

It’s not a good idea to make the entire server wait for one query. It’s much better to use callback functions when fetching data.

That’s why I said it was bad practice.

ah, sorry missed reading that part.