[help] MySQL query (overextended) problem

I’m trying to make a function that checks whether the identifier is present in the database or not via a command, the problem is that if the result is null (so the identifier is not present in the database) it doesn’t return anything to me. (I am using overexented)

(sorry for my english)

Code:

Server Side

-- FUNCTION THAT CHECK IF THE IDENTIFIER EXIST IN THE DB (WHERE THE PROBLEM SHOW UP)

function checkIdentifier(identifier)
    MySQL.query('SELECT * FROM gnc_diamond WHERE identifier = ?', {identifier}, function(result)
        if result then
            for _, v in pairs(result) do
                print(v.identifier)
                print("Identifier already exist")
            end
        else -- THE PROBLEM IS HERE, IF THE IDENTIFIER DOESN'T EXIST IT DOESN'T PRINT ANYTHING
            print("The identifier doesn't exist in the db")
            -- Insert a new entry in the db
        end
    end)
end

-- COMMAND

lib.addCommand('group.admin', {'addgem', 'givegem'}, function(source, args)

    checkIdentifier(getIdentifier(args.target))

end, {'target:number', 'gem:number'})

-- FUNCTION THAT CHECK THE PLAYER IDENTIFIER

function getIdentifier(source)

    local license = GetPlayerIdentifier(source, 1)
    local identifier = string.gsub(license, "license", "char1")

    return identifier
end

This is most likely because you are checking “if result isn’t nil… else…”, but result won’t be nil. It will just be an empty table if there is no identifier. So replace “if result then” with “if next(result) then”, which means “if the result table is not empty”.
Like this;

function checkIdentifier(identifier)
	MySQL.query('SELECT * FROM gnc_diamond WHERE identifier = ?', {identifier}, function(result)
		if next(result) then -- previously "if result then"
			for _, v in pairs(result) do
				print(v.identifier)
				print("Identifier already exist")
			end
		else
			print("The identifier doesn't exist in the db")
			-- Insert a new entry in the db
		end
	end)
end
1 Like

This is trapmouth, deezy, Ive joined this server and I thought it was great but ive mustve been wilding being on booz, instagram me @ thabunnyhop and see that im a good rp type of guy. Check my deets and lemme join.??

thanks again for the help dude!