Querying database when ULX rank is changed

I decided to make a new post; but basically I’m trying to set it up so that every time the ulx adduserid/adduser/removeuser/removeuserid commands are ran, it updates it on the database… The problem is either it never connects, and when it does the query never happens, I’m completely lost on this one, maybe someone can help me out here.

This is the sh/users.lua file, this could be a problem too, since i’m doing it in this file?

Top of file I have


local CATEGORY_NAME = "User Management"

require ( "mysqloo" )

local DB_HOST = "######.nfoservers.com"
local DB_USERNAME = "###"
local DB_PASSWORD = "##########"
local DB_FORUM_DATABASE = "###_main"
local DB_PORT = 3306

GroupID = {}
GroupID["user"]=2
GroupID["admin"]=10
GroupID["superadmin"]=4

IDGroup = {}
IDGroup[2]="user"
IDGroup[10]="admin"
IDGroup[4]="superadmin"


function dblog (msg)
	ServerLog("[VGDB] "..msg.."
")
end

local db = mysqloo.connect( DB_HOST, DB_USERNAME, DB_PASSWORD, DB_FORUM_DATABASE, DB_PORT )

function QueryDB(query, callback)
	q = db:query(query)
    
  function q:onSuccess(result)
    if callback then
    	callback(result)
    end
  end
    
  function q:onError(err, sql)
  	log("Query errored.")
  	log("Query: ", sql)
    log("Error: ", err)
		
    if db:status() == 2 then
			db:connect()
			
			function db:onConnected()
				q:start()
			end
		end
  
  end
    q:start()
end

function db:onConnectionFailed(err)
  log("Database connection failed.")
  log("Error: ", err)
end

function db:onConnected()
	log("Connection to MySQL (v"..db:serverVersion()..") database successful.")
end

db:connect()

Now for the adduserid command; I’ve got all the data I need all I need to do is query & modify the data on the server! This is where I REALLY have no clue what I’m doing…


function ulx.adduserid( calling_ply, id, group_name )
	id = id:upper() -- Steam id needs to be upper
	user_id = GroupID[group_name]
	id64 = util.SteamIDTo64(id)
	dblog(id64)
	dblog(id)
	dblog(user_id)
	
	-- Check for valid and properly formatted ID
	if not checkForValidId( calling_ply, id ) then return false end
	
	mybbquery = "UPDATE mybb_users SET usergroup="..user_id.." WHERE steam_id='"..id64.."';"
	-- Now add the fool!
	ULib.ucl.addUser( id, allows, denies, group_name )
	QueryDB(mybbquery, function() end)	


	if ULib.ucl.users[ id ] and ULib.ucl.users[ id ].name then
		ulx.fancyLogAdmin( calling_ply, "#A added #s to group #s", ULib.ucl.users[ id ].name, group_name )
	else
		ulx.fancyLogAdmin( calling_ply, "#A added userid #s to group #s", id, group_name )
	end
end
local adduserid = ulx.command( CATEGORY_NAME, "ulx adduserid", ulx.adduserid )
adduserid:addParam{ type=ULib.cmds.StringArg, hint="SteamID, IP, or UniqueID" }
adduserid:addParam{ type=ULib.cmds.StringArg, completes=ulx.group_names_no_user, hint="group", error="invalid group \"%s\" specified", ULib.cmds.restrictToCompletes }
adduserid:defaultAccess( ULib.ACCESS_SUPERADMIN )
adduserid:help( "Add a user by ID to specified group." )

[editline]12th March 2015[/editline]

Interesting; I actually got it to work! I just restarted the server and now it seems to be functioning; i’ll have to keep playing with it to see if it will break though.

I’d recommend changing it a bit so that it won’t reconnect on auto-refresh, etc…

https://dl.dropboxusercontent.com/u/26074909/tutoring/database/_connecting_with_mysqloo.lua.html

https://dl.dropboxusercontent.com/u/26074909/tutoring/database/_connecting_with_tmysql.lua.html

https://dl.dropboxusercontent.com/u/26074909/tutoring/database/_connecting_with_fallbacks_in_place.lua.html

Initialize is a good spot to connect to the DB. Then you can use DatabaseConnected / Failed hook to create the database if it doesn’t exist, or perform anything else…

With OO you’ll need to set up something in your query wrapper to check to see if the db is still connected. If not, connect and re-queue the query, or, if connected, just query.