Help with mySQL:<

Edit: Fixed, no help needed

Hey, i am learning lua and trying to make a gamemode and i got stuck on implementing sql to it. Shit just wont work, no idea why, please take a look on my code and thanks in advance.
I was trying to get it to work for a while now and got many different errors, when i run this current code i end up with this http://puu.sh/1HmpF heres the line 21 http://puu.sh/1HmsK same as on screenshot is down in the code.



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

function checkQuery(query)
	
    local playerInfo = query:getData()
    if playerInfo[1] ~= nil then
	return true
    else
	return false
    end
	
end

function db:onConnected()

	print("[MySQL] Connected to database  on "..DB_HOST..":"..DB_PORT.." and version "..db:serverInfo())

end

function db:onConnectionFailed( err )

	print("[MySQL] Failed to connect to database on "..DB_HOST..":"..DB_PORT)
	print( "Error:", err )

end

function LoadPly( ply )
	
	-- if db:status() == DATABASE_NOT_CONNECTED then print("[MySQL] Not connected to database while trying to load player \"" .. ply:Nick() .. "\", trying to re-connect.") db:connect() db:wait() end -- Moved this to PlayerInitialSpawn
	local query1 = db:query( "SELECT * FROM `players` WHERE `SteamID` = '"..ply:SteamID().."';" )
	function query1:onSuccess( data )
	
		if not checkQuery(q)  then
		
			print("[MySQL]: Player \""..ply:Nick().."\" loaded from database.")
			PrintTable( data )
		
		else
		
			local query2 = db:query("INSERT INTO `players` (`Nick`, `SteamID`, `Money`, `Job`) VALUES ('" .. ply:Nick() .. "', '" .. ply:SteamID() .. "', " .. 200 .. ", " .. 1 .. ")")
			query2.onSuccess = function(q)  print("[MySQL]: Player \""..ply:Nick().."\" added to the database.") end
			query2.onError = function(q,e) print("[MySQL]: Error: "..e) end
			query2:start()	
		
		end	
		
	end
 
	function query1:onError( err, sql )

		print( "[MySQL]: Query error while trying to load player \""..ply:Nick().."\" from the database!" )
		print( "Query:", sql )
		print( "Error:", err )

	end

	query1:start()
		
end
	

function Initialize()
	
	db:connect()
	db:wait()
	
end

hook.Add("Initialize", "Initialize", Initialize)
 

function GM:PlayerInitialSpawn( ply )
	
	if db:status() == DATABASE_NOT_CONNECTED then print("[MySQL] Not connected to database while trying to load player \"" .. ply:Nick() .. "\", trying to re-connect.") db:connect() db:wait() end
	LoadPly( ply )
	
end


Because

[lua]
if not checkQuery(q) then
[/lua]

should be

[lua]
if not checkQuery(query1) then
[/lua]

From what I can see. I have no deeper insight in the sql.
All I can see is that q isn’t defined.

Change [lua]if not checkQuery(q) then[/lua] to [lua]if not data[1] then[/lua]

No need for the function, also no need for :getData() when the onSuccess function already returns the data, so that’s redundant.

Also instead of comparing a variable to “nil”, you can just use “if data[1] then” or “if not data[1]”, I prefer to use “not” rather than “!”, but it’s a personal choice.



function GM:PlayerInitialSpawn( ply )
	
	if db:status() == DATABASE_NOT_CONNECTED then print("[MySQL] Not connected to database while trying to load player \"" .. ply:Nick() .. "\", trying to re-connect.") db:connect() db:wait() end
	LoadPly( ply )
	
end


Don’t check db:status() before each query, it is a blocking function and may cause lag. Also, unless you did otherwise, DATABASE_NOT_CONNECTED isn’t defined anywhere and the condition will be never true as db:status() will never be nil. You need to use (…) == mysqloo.DATABASE_NOT_CONNECTED.
(source:

)



local query2 = db:query("INSERT INTO `players` (`Nick`, `SteamID`, `Money`, `Job`) VALUES ('" .. ply:Nick() .. "', '" .. ply:SteamID() .. "', " .. 200 .. ", " .. 1 .. ")")


If I set my name to '); TRUNCATE TABLE players;, I will ruin your database. Use db:escape(stringValue) to make sure noone does that.

Ah yes, I didn’t go into reading the full code, but he is right, you will need to escape your player’s nicknames or they can use sql injections on your server, this includes anything to do with any custom input, such as chat messages, nicknames, etc.

Wow thank you all guys, ye i had some stupid errors there and the code was from tutorials which were old and they sucked :confused: thanks for reminding me to escape the queries.