Problems making a console command.

[lua]
require( “mysqloo” )

function GivePointsCommand( args )

local db = mysqloo.connect( )

function db:onConnected()
	print("GivePoints: MySQL Connected!")		
	
	db:wait()
	
	local q = self:query("UPDATE pointshop_data SET points=points+" .. args[2] .. " WHERE steamid='" .. args[1] .. "'")
	
	function q:onSuccess( data )
		print("GivePoints: Points given.")
	end
	
	function q:onError( err, sql )
		print("GivePoints: Error!")
		print("Query:", sql )
		print("Error:", err )
	end
	
	function db:onConnectionFailed( err )
		print("GivePoints: Connection to DB failed!")
		print("Error:", err)
	end
end

end
concommand.Add( “ps_givepoints”, GivePointsCommand )
[/lua]

The problem I am having with this is when I run the command, it prints nothing to the server console, and the query doesn’t get ran. However, there are no errors either. It just returns nothing.

The command I was running: ps_givepoints STEAM_0:1:19050040 1000

If you guys could help me out that would be great.

You’re missing the final :connect call: https://dl.dropboxusercontent.com/u/26074909/tutoring/database/_connecting_with_mysqloo.lua.html

[lua]
require( “mysqloo” )

print(“GivePoints Loaded!”)

function GivePointsCommand( args )

if args[1] == "" then
	print("GivePoints:You need to add some arguments!")
end

local db = mysqloo.connect( db deets ofc )

function db:onConnected()
	print("GivePoints: MySQL Connected!")		
	
	local q = self:query("UPDATE pointshop_data SET points=points+" .. args[2] .. " WHERE steamid='" .. args[1] .. "'")
	
	function q:onSuccess( data )
		print("GivePoints: Points given.")
	end
	
	function q:onError( err, sql )
		print("GivePoints: Error!")
		print("Query:", sql )
		print("Error:", err )
	end
	
	function db:onConnectionFailed( err )
		print("GivePoints: Connection to DB failed!")
		print("Error:", err)
	end
end

db:connect()

end
concommand.Add( “ps_givepoints”, GivePointsCommand )
[/lua]

This is what I have now. It still returns nothing. :confused:

Any time you do a query, it goes through a call-back. It won’t “return” because by the time the return is called, the pointer will have already passed the function call so it’ll ignore it.

So those prints won’t work?

They should, but they won’t appear immediately.

As a side note, I recommend adding a “return” into your arg check to prevent errors.

If you look at my tutorial, it shows me using hook.Call; I do this so code that needs to follow those specific actions onSuccess or onFailure can be executed sequentially instead of stopping all execution and waiting on a return. I’d recommend something like that, at least for initial connects. For other queries, create helper functions with the specific actions and specific functions to execute.

Am I missing something obvious here? No one has pointed out that when you add a concommand, the arguments for the function are ply,cmd,args and not just args,

[lua]function GivePointsCommand( args )

--snip--

print(args) --this will actually print the player that run the function!

end
concommand.Add( “ps_givepoints”, GivePointsCommand )[/lua]