Hi,
I’m trying to learn more how to work with table, something I’m awful at. Basically, I would like to pick up a specific string/info from already preloaded table from database. All that based on player’s identifier.
Something like this below:
Table = {}
CreateThread(function() -- trigger function for loading the table from the database
loadTable()
end)
function loadTable(cb) -- the function
Table = {}
local result = exports.oxmysql:fetchSync('SELECT * FROM info', {})
if result ~= nil then
for k,v in ipairs(result) do
Table[v.license] = {v.license, v.A, v.B}
print(v.license, v.A, v.B)
end
if cb then
cb()
end
end
end
-- test command where the argument simulates specific identifier
RegisterCommand("pickfromtable", function(source, args, rawCommand)
local PlayerLicense = args[1]
for k,v in ipairs(Table) do
if v == PlayerLicense then
print(v.A, v.B) -- values I need to be able to pick up
else
print('error')
end
end
end, false)
CREATE TABLE IF NOT EXISTS `info` (
`license` varchar(100) NOT NULL,
`A` varchar(50) DEFAULT NULL,
`B` varchar(50) DEFAULT NULL,
PRIMARY KEY (`license`)
)
INSERT INTO `info` (`license`, `A`, `B`) VALUES
('fivem:11111', 'Something', '56161'),
('fivem:22222', 'Anything', '1516'),
('fivem:33333', 'Whatever', '13216546');
How can I make something like this to work? Any advice will be appreciated!