Nikolay_888 Posted August 28, 2015 Share Posted August 28, 2015 Hello. I create a function, that return data from database. function takePlayerDataFromDB (id, data) if id and data then local receivedData = false local query1 = dbQuery (function (query) local poll, num_rows = dbPoll (query, 0) if poll then if num_rows == 1 then for _, row in pairs ( poll ) do receivedData = row[data] end end end end, players_db, "SELECT ?? FROM players WHERE id=?", data, id) return receivedData end end a = takePlayerDataFromDB (5, 'name') The problem is line 14 runs faster then line 9, and function returns false. And I don't know how to use 'return' right in callback function. I would be grateful for help. Link to comment
JR10 Posted August 28, 2015 Share Posted August 28, 2015 You need coroutines in order to do that. Some tutorials: http://www.lua.org/pil/9.html http://lua-users.org/wiki/CoroutinesTutorial If you're still a beginner or you couldn't understand what coroutines are then I'd suggest you find a different way to do what you're doing. Link to comment
Noki Posted August 28, 2015 Share Posted August 28, 2015 db = dbConnect(args) function getDataFromDb(id, column) if id and column then qh = dbQuery(db, "SELECT `??` FROM `players` WHERE `id`=?", column, id) return dbPoll(qh, 0) end end Why not use something like that, then loop through the table to get your needed data? Link to comment
Nikolay_888 Posted August 28, 2015 Author Share Posted August 28, 2015 Noki, i read that using dbQuery without callback function will freeze server. I searched this topic viewtopic.php?f=91&t=86184&p=780099&hilit=coroutine#p780099 This is the same situation, but I can't use this code for my situation yet. Link to comment
Noki Posted August 28, 2015 Share Posted August 28, 2015 dbQuery does not freeze the server in the way you think it does. It's more so saying that you won't be able to query the database while the result is being fetched. Your server will still work fine. Correct me if I'm wrong. Try it yourself. Link to comment
JR10 Posted August 28, 2015 Share Posted August 28, 2015 @Noki Because the data can be not ready yet to be returned. And using -1 instead of 0 will freeze everything until it is returned. Link to comment
Nikolay_888 Posted August 28, 2015 Author Share Posted August 28, 2015 Oh, I did it. Thank you very much, JR10. This is my code. It works, but is this code right? I used coroutines first time and try 1000 variants function someFunction () playerco = coroutine.create(function (data) outputChatBox (data, root) end) coroutine.resume (getdataco, 57, 'name') end --- getdataco = coroutine.create (function (id, dataDB) local data = false function querrrry () local query1 = dbQuery (function (query) local poll, num_rows = dbPoll (query, 0) if poll then if num_rows == 1 then for _, row in pairs ( poll ) do data = row[dataDB] coroutine.resume (getdataco) end end end end, players_db, "SELECT ?? FROM players WHERE id=?", dataDB, id) end querrrry () coroutine.yield () coroutine.resume (playerco, data) end) Link to comment
JR10 Posted August 28, 2015 Share Posted August 28, 2015 Why are you declaring another function inside the coroutine function? And why do you need 2 coroutines? Something like this should do: local co = coroutine.create(function() dbQuery(callback, database, 'SELECT * FROM `table`') local results = coroutine.yield() -- Pause execution until coroutine.resume is called -- use results end) coroutine.resume(co) -- Start running the coroutine function for the first time function callback(qh) local results = dbPoll(qh, 0) coroutine.resume(co, results) end Link to comment
Nikolay_888 Posted August 28, 2015 Author Share Posted August 28, 2015 Function someFunction shows using getdataco in real situation. That is getdataco possible use in another situations. So, first coroutine need to use received data after receive. Link to comment
JR10 Posted August 28, 2015 Share Posted August 28, 2015 Well, for your problem, my code would just do for you. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now