Jump to content

Get the player element on serverside script.


harryh

Recommended Posts

Hi, I am creating this dog script. I need to get the players database number which is saved via getElementData(player element, "databaseid").

 

function loadPlayerInfo(thePlayer)
	local dbid = getElementData(thePlayer, "databaseid")
	getDInfo = mysql:query_fetch_assoc("SELECT * FROM dogs WHERE pID = '" ..dbid.."'")
	local result = mysql:free_result(getDInfo)
	InfoArray = result
	if (InfoArray == true) then
		
	elseif (InfoArray == false) then
		--No dog saved to DB
		print("No Dog In DB.")
		print(InfoArray)
	else
		print(InfoArray)
		print("Fatal Error has occured please contact administrator.")
	end
end
addCommandHandler("loaddogs", loadPlayerInfo)

This always returns false when information is in the database as I cannot get the player element where thePlayer is the element.

Also there is data in the MySQL database and it is correct.

 

Thanks

Edited by harryh
Link to comment
  • Moderators

mysql:free_result doesn't return any value ! You want to fetch each row from the result by doing calls to mysql:fetch_assoc until it returns nil.

function loadPlayerInfo(thePlayer)
	local dbid = getElementData(thePlayer, "databaseid")
	local result = mysql:query_fetch_assoc("SELECT * FROM dogs WHERE pID = '" ..dbid.."'")
	local dogs = {}
	if result then
		while true then
			local row = mysql:fetch_assoc( result )
			if not row then break end
			table.insert(dogs, row) -- add the sql row to dogs list
		end
		mysql:free_result(result) -- important to prevent memory leak
	end
	
	if (dogs and #dogs > 0) then
		print("Found "..#dogs.." dog(s) for pID "..tostring(dbId))
		print(dogs)
	else
		--No dog saved to DB
		print("No Dog In DB.")
	end
end
addCommandHandler("loaddogs", loadPlayerInfo)

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...