Jump to content

Not iterating through My SQL results adding objects.


harryh

Recommended Posts

Right so I have objects loaded into the server database.

Like below:

id mid x y z rox roy roz ds

I want to check the table and go through it adding all the objects inside.

row one createobject(mid,x,y,z,rox,roy,roz,ds)

row two createobject(mid,x,y,z,rox,roy,roz,ds)

etc untill no rows left. I have this but it is not working and returning attempt to call nil value even though there are row in the table:

function loadObjects()
	local result = mysql:query("SELECT * FROM landobjects")
	if (result) then
		if (loaded == 0) then
			
			for result,row in mysql:rows_assoc(result) do
				if (not row) then break end
				local mid = row["mid"]
				local ox = row["ox"]
				local rox = row["rox"]
			
				local oy = row["oy"]
				local roy = row["roy"]

				local oz = row["oz"]
				local roz = row["roz"]


				local object = createObject( mid, oz, oy, oz, rox, roy, roz)

				local ds = function() if (row["ds"] == 1) then setElementDoubleSided( object, true ) else setElementDoubleSided( object, false ) end end
				
				table.insert(LCobs, object)
				triggerClientEvent( "objectStream", client, object, mid )
			end
			loaded = 1
		else
			clearLoadedObjects()
			loadObjects()
			loaded = 0
			outputChatBox( "Objects Reloaded" )
		end
	end
	outputChatBox( "No Objects to Load")
end
addCommandHandler( "loadall", loadObjects )

 

Edited by harryh
Link to comment
5 hours ago, LopSided_ said:

Try using MTA's own MySQL functions, much easier

https://wiki.multitheftauto.com/wiki/DbConnect

Anyway, in the code you provided you haven't established a connection with the database, so it can't find the table.

connection = mysql_connect(blah blah blah)

query = mysql_query(connection,"SELECT * FROM landobjects")

if query then
  for query, row in mysql_rows_assoc(query) do
    model = row["mid"]
    end
end

  

If I am correct is this suppose to select all the row. 

Then in each row get column mid and return it as variable model. How come this does not either error nil value or Boolean when there is information in the database.

i have wrote this on phone so sorry for any mistakes

Link to comment

@harryh, try using the in-built functions of MTA. That module is old already, and the in-built functions are much better.

local dbConnection = dbConnect(...)
local nrQuery = dbQuery(dbConnection, ...)
local rQuery = dbPoll(nrQuery, -1)

for i, k in ipairs(rQuery) do
	*do something with the query*
end

 

Edited by NeXuS™
  • Like 1
Link to comment
5 hours ago, NeXuS™ said:

@harryh, try using the in-built functions of MTA. That module is old already, and the in-built functions are much better.


local dbConnection = dbConnect(...)
local nrQuery = dbQuery(dbConnection, ...)
local rQuery = dbPoll(nrQuery, -1)

for i, k in ipairs(rQuery) do
	*do something with the query*
end

 

Still returning rQuery as false there going to outputchatbox("No Objects to load"). The connection is working because I can use it to insert data.

 

local hostname = "127.0.0.1"
local username ="root"
local password =  ""
local database = "testserver"


local sqlConnection = dbConnect( "mysql", "dbname=".. database ..";host="..hostname.."", ""..username.."",""..password.."" )

function loadObjects()
	local nrQuery = dbQuery(sqlConnection, "SELECT * FROM landobjects")
	local rQuery = dbPoll( nrQuery, -1 )
	if (rQuery) then
		if (loaded == 0) then
			for _, row in ipairs(rQuery) do

				if (not row) then break end


				local mid = row["mid"]
				if (not mid) then outputChatBox( "No model ID", getRootElement(  )) break end
				local ox = row["ox"]
				local rox = row["rox"]
			
				local oy = row["oy"]
				local roy = row["roy"]

				local oz = row["oz"]
				local roz = row["roz"]


				local object = createObject( mid, oz, oy, oz, rox, roy, roz)

				local ds = function() if (row["ds"] == 1) then setElementDoubleSided( object, true ) else setElementDoubleSided( object, false ) end end
				
				table.insert(LCobs, object)
				triggerClientEvent( "objectStream", client, object, mid )
				loaded = 1
			end
		else
			clearLoadedObjects()
			loadObjects()
			loaded = 0
			outputChatBox( "Objects Reloaded" )
		end
	end
	outputChatBox( "No Objects to Load")
end
addCommandHandler( "loadall", loadObjects )

Here is the DB

fe8d08ba1b6343d0953051215118affc.png

Link to comment

It's not returning false. Your if statement is :Oed up.

local hostname = "127.0.0.1"
local username ="root"
local password =  ""
local database = "testserver"


local sqlConnection = dbConnect( "mysql", "dbname=".. database ..";host="..hostname.."", ""..username.."",""..password.."" )

function loadObjects()
	local nrQuery = dbQuery(sqlConnection, "SELECT * FROM landobjects")
	local rQuery = dbPoll( nrQuery, -1 )
	if (rQuery) then
		if (loaded == 0) then
			for _, row in ipairs(rQuery) do

				if (not row) then break end


				local mid = row["mid"]
				if (not mid) then outputChatBox( "No model ID", getRootElement(  )) break end
				local ox = row["ox"]
				local rox = row["rox"]
			
				local oy = row["oy"]
				local roy = row["roy"]

				local oz = row["oz"]
				local roz = row["roz"]


				local object = createObject( mid, oz, oy, oz, rox, roy, roz)

				local ds = function() if (row["ds"] == 1) then setElementDoubleSided( object, true ) else setElementDoubleSided( object, false ) end 			end
				
				table.insert(LCobs, object)
				triggerClientEvent( "objectStream", client, object, mid )
				loaded = 1
			end
		else
			clearLoadedObjects()
			loadObjects()
			loaded = 0
			outputChatBox( "Objects Reloaded" )
		end
	else
		outputChatBox( "No Objects to Load")
	end
end
addCommandHandler( "loadall", loadObjects )

 

Edited by NeXuS™
  • Like 1
Link to comment

Right Thank you very much so it is spawning but It is not visible I have used getelementmodel to check to see if the object is being created and it is but just cannot see it any idea why i have tried using setelementinterior 0 and setelementdimension 0 

 

function loadObjects()
	local nrQuery = dbQuery(sqlConnection, "SELECT * FROM landobjects")
	local rQuery = dbPoll( nrQuery, -1 )
	if (rQuery) then
		if (loaded == 0) then
			for _, row in ipairs(rQuery) do
				print(row["ox"])

				if (not row) then outputChatBox( "no row", getRootElement()) break end



				local mid = row["mid"]
				if (not mid) then outputChatBox( "No model ID", getRootElement(  )) break end
				local ox = row["ox"]
				local rox = row["rox"]
			
				local oy = row["oy"]
				local roy = row["roy"]

				local oz = row["oz"]
				local roz = row["roz"]


				local object = createObject( mid, oz, oy, oz, rox, roy, roz, false)
				setElementDimension( object, 0 )
				setElementInterior( object, 0)
				print(getElementModel( object ))

				if (row["ds"] == 1) then 
					setElementDoubleSided( object, true ) 
				else 
					setElementDoubleSided( object, false ) 
				end 			
				
				table.insert(LCobs, object)
				triggerClientEvent( "objectStream", getRootElement(  ), object, mid )
				loaded = 1
			end
		else
			clearLoadedObjects()
			loadObjects()
			loaded = 0
			outputChatBox( "Objects Reloaded" )
		end
    else
    	outputChatBox( "No Objects to Load")
    	loaded = 0
	end
end
addCommandHandler( "loadall", loadObjects )

 

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...