Jump to content

Question


FlyingSpoon

Recommended Posts

So I want to get some data from my database and insert it into a table or make it into a table so I can run a few things e.g.

local general = {}

addEventHandler("onResourceStart", root,
function()
	local q = dbQuery(connection, "SELECT * FROM table")
	local r = dbPoll(q, -1)
	
	general = {
	{r["first"], r["second"]}
	}
end)

function run()
for i, v in ipairs(general) do
  	outputChatBox(v[1].." - "..v[2], player)
end
addCommandHandler("runme", run)

How would I go about doing this?

Link to comment
addEventHandler("onResourceStart", root,
function()
	local q = dbQuery(function(nrQ)
		local rQ = dbPoll(nrQ, 0)
		for i, k in pairs(rQ) do
			table.insert(general, {k["first"], k["second"]})
		end
	end, connection, "SELECT * FROM table")
end)

I'd never use dbPoll with -1 timeout, as it can cause freezes. dbPoll returns a table of all data,

{
	{
		["first"] = "first", 
		["second"] = "second"
	}, 
	{
		...
	},  
	{
		...
	}
}

so you have to loop through it.

Edited by NeXuS™
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...