How to pick up strings from a table based on player's identifier?

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!

Quick answer - use the WHERE clause

I’m too rusty with SQL so I cant recall the exact way to use it off the top of my head so I suggest looking at already existing examples - there’s a load of those in different ESX resources that talk to a SQL table to store and retrieve data.

Yes, I know how to use that.

But I would like to load the whole table only once per session and then being able to take the info without accessing database over and over again.

Anyway, I’ll take a quick break and gonna get back to it. I might have just overlooked something I already know, lol.

Solution provided by szymczakovv#1937 in CFX Discord

local Table = {}
loadTable = function(cb)
    local result = exports.oxmysql:fetchSync('SELECT * FROM info', {})
    if result then
        for i=1, #result, 1 do
            Table[#Table+1] = {license = result[i].license, A = result[i].A, B = result[i].B} -- "Table[#Table+1] = data" works like table.insert
        end
    end
    if cb then
        cb()
    end
end


To receive data for a given license from the table, you can do it like this:
RegisterCommand('pickfromtable', function(source,args,raw)
    for i=1, #Table, 1 do
        if (args[1] == Table[i].license) then
            print(Table[i].A, Table[i].B);
        else
            print('not found')
        end
    end
end)