• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Need some help with a function using a simple SQL query

Bintzer

One Love
Joined
Dec 19, 2007
Messages
1,763
Reaction score
30
Location
Orlando
Well as the thread title states I need some help with a function.
Code:
function setGuildStorage(cid,  key, value)    
db.query("DELETE FROM `guild_storage` WHERE `guild_id` ='" .. getPlayerGuildId(cid) .. "' AND `key` ='" .. key .. "';")
    db.query("INSERT INTO `guild_storage` VALUES (" .. getPlayerGuildId(cid) .. "," .. key .."," .. value .. ");")
end
-------------
function getGuildStorage(cid, key)
local keys = db.getResult("SELECT `value` FROM `guild_storage` WHERE `guild_id` = '" .. getPlayerGuildId(cid) .. "' AND `key` = '" .. key .. "'")		
	if keys:getID() ~= -1 then
		keys:free()
	end
	return keys:getDataInt("value")
end
I know the main problem is in the keys:getDataInt as I that's the error I'm getting in the console (result is not being set)
and I don't really know how to use db functions, just going off other examples..
I think its pretty self-explanatory on what its supposed to do.

Some help anybody? :$ (I'd love an explanation too)

p.s. sorry for not using LUA tags, it capitalizes a bunch of the script for some reason..
 
Last edited:
Alright,
Lua:
function onSay(cid, words, param, channel) 
   local challengedID = getGuildId(param)
    local guildName = getPlayerGuildName(cid)
    if (getGuildStorage(cid, GW_STORAGE_RED) <= 1 and getGuildStorage(cid, GW_STORAGE_BLUE) <= 1 and guildName ~= 0) then
        if(param ~= '' and challengedID ~= 0) then
            setGuildStorage(cid, GW_STORAGE_RED, 1)
            for _, cid in ipairs(getPlayersOnline()) do
                if getGuildStorage(cid, GW_STORAGE_RED) == 1 then
                    doPlayerSendTextMessage(cid, MESSAGE_STATUS_DEFAULT,"Your guild has challenged the  " .. param .. " to a fight to the death in the arena!  Waiting for them to accept.")
                elseif guildName == (param) then
                    doPlayerSendTextMessage(cid, MESSAGE_STATUS_DEFAULT,"Your guild has been challenged by the  " .. guildName .. " to fight to the death in the arena!  If you wish to accept say !accept.")
                    setGuildStorage(cid, GW_STORAGE_BLUE,1)
                else
                    doPlayerSendTextMessage(cid, MESSAGE_STATUS_DEFAULT,"The " .. guildName .. " have invited the " .. param .. " to fight to the death in the arena!")
                end
            end
        else
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_DEFAULT, "The " .. param .. " is not a valid guild.  Check your spelling.")
        end
    else
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Your guild is already fighting in the Guild War Arena.")
    end
    return true
end
Here's a ss of the console errors:
28upe3c.png
 
Try this script

Lua:
function onSay(cid, words, param)        
   local challengedID = getGuildId(param)
    local guildName = getPlayerGuildName(cid)
    if (getGuildStorage(cid, GW_STORAGE_RED) <= 1 and getGuildStorage(cid, GW_STORAGE_BLUE) <= 1 and guildName ~= 0) then
        if(param ~= '' and challengedID ~= 0) then
            setGuildStorage(cid, GW_STORAGE_RED, 1)
            for _, cid in ipairs(getPlayersOnline()) do
                if getGuildStorage(cid, GW_STORAGE_RED) == 1 then
                    doPlayerSendTextMessage(cid, MESSAGE_STATUS_DEFAULT,"Your guild has challenged the  " .. param .. " to a fight to the death in the arena!  Waiting for them to accept.")
                elseif guildName == (param) then
                    doPlayerSendTextMessage(cid, MESSAGE_STATUS_DEFAULT,"Your guild has been challenged by the  " .. guildName .. " to fight to the death in the arena!  If you wish to accept say !accept.")
                    setGuildStorage(cid, GW_STORAGE_BLUE,1)
                else
                    doPlayerSendTextMessage(cid, MESSAGE_STATUS_DEFAULT,"The " .. guildName .. " have invited the " .. param .. " to fight to the death in the arena!")
                end
            end
        else
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_DEFAULT, "The " .. param .. " is not a valid guild.  Check your spelling.")
        end
    else
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Your guild is already fighting in the Guild War Arena.")
    end
    return true
end

or in your 004-database.lua
in data/lib

Result Not set.. in your query. db.query
Try change for this.
data/lib/004-database.lua
change for

Lua:
if(result == nil) then
	print("> WARNING: Couldn't load database lib.")
	return
end

Result = createClass(nil)
Result:setAttributes({
	id = -1,
	query = ""
})

function Result:getID()
	return self.id
end

function Result:setID(_id)
	self.id = _id
end

function Result:getQuery()
	return self.query
end

function Result:setQuery(_query)
	self.query = _query
end

function Result:create(_query)
	self:setQuery(_query)
	local _id = db.storeQuery(self:getQuery())
	if(_id) then
		self:setID(_id)
	end

	return self:getID()
end

function Result:getRows(free)
	local free = free or false
	if(self:getID() == -1) then
		error("[Result:getRows] Result not set!")
	end

	local c = 0
	repeat
		c = c + 1
	until not self:next()

	local _query = self:getQuery()
	self:free()
	if(not free) then
		self:create(_query)
	end

	return c
end

function Result:getDataInt(s)
	if(self:getID() == -1) then
		error("[Result:getDataInt] Result not set!")
	end

	return result.getDataInt(self:getID(), s)
end

function Result:getDataLong(s)
	if(self:getID() == -1) then
		error("[Result:getDataLong] Result not set!")
	end

	return result.getDataLong(self:getID(), s)
end

function Result:getDataString(s)
	if(self:getID() == -1) then
		error("[Result:getDataString] Result not set!")
	end

	return result.getDataString(self:getID(), s)
end

function Result:getDataStream(s)
	if(self:getID() == -1) then
		error("[Result:getDataStream] Result not set!")
	end

	return result.getDataStream(self:getID(), s)
end

function Result:next()
	if(self:getID() == -1) then
		error("[Result:next] Result not set!")
	end

	return result.next(self:getID())
end

function Result:free()
	if(self:getID() == -1) then
		error("[Result:free] Result not set!")
	end

	self:setQuery("")
	local ret = result.free(self:getID())
	self:setID(-1)
	return ret
end

Result.numRows = Result.getRows
function db.getResult(query)
	if(type(query) ~= 'string') then
		return nil
	end

	local ret = Result:new()
	ret:create(query)
	return ret
end
 
I deleted channel from the onSay function (looks like thats all you did unless i missed something)
and I already had this exact same 004-database.lua file

I think the problem might be in the table's structure.. take a look:
2en5gzq.png


edit: fixed! i just changed the return line in getguildstorage to keys:getID("value") instead of getDataInt
thanks anyways :)
 
Last edited:
Back
Top