Jump to content

Function return data from DB


Nikolay_888

Recommended Posts

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
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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...