harryh Posted March 30, 2017 Share Posted March 30, 2017 (edited) 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 March 30, 2017 by harryh Link to comment
Administrators Lpsd Posted March 31, 2017 Administrators Share Posted March 31, 2017 (edited) 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. Edited March 31, 2017 by LopSided_ Link to comment
Rondawgb Posted March 31, 2017 Share Posted March 31, 2017 The connection is established from another resource and we know its connected because its storing data properly, the issue is when hes asking it to create the objects that are stored its returning the error "19:attempt to call a boolean value" Link to comment
harryh Posted March 31, 2017 Author Share Posted March 31, 2017 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
NeXuS™ Posted March 31, 2017 Share Posted March 31, 2017 (edited) @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 March 31, 2017 by NeXuS™ 1 Link to comment
harryh Posted March 31, 2017 Author Share Posted March 31, 2017 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 Link to comment
NeXuS™ Posted March 31, 2017 Share Posted March 31, 2017 Is it up on your phpMyAdmin? Link to comment
harryh Posted March 31, 2017 Author Share Posted March 31, 2017 4 minutes ago, NeXuS™ said: Is it up on your phpMyAdmin? What do you mean up, if you mean is the data loaded onto phpmyadmin then yes. I use MYSQL workshop to access the server db as easier than phpmyadmin Link to comment
NeXuS™ Posted March 31, 2017 Share Posted March 31, 2017 Try outputing the number of elements inside your table. outputChatBox(#rQuery) Link to comment
harryh Posted March 31, 2017 Author Share Posted March 31, 2017 2 minutes ago, NeXuS™ said: Try outputing the number of elements inside your table. outputChatBox(#rQuery) Returns 1 so it is checking so why is rQuery return false then? Link to comment
NeXuS™ Posted March 31, 2017 Share Posted March 31, 2017 (edited) 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 March 31, 2017 by NeXuS™ 1 Link to comment
harryh Posted March 31, 2017 Author Share Posted March 31, 2017 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
NeXuS™ Posted March 31, 2017 Share Posted March 31, 2017 Try outputing it's position. 1 Link to comment
harryh Posted March 31, 2017 Author Share Posted March 31, 2017 4 minutes ago, NeXuS™ said: Try outputing it's position. Thanks @NeXuS™ made a c0ckup had oz instead of ox. local object = createObject( mid, oz, oy, oz, rox, roy, roz, false) Link to comment
NeXuS™ Posted March 31, 2017 Share Posted March 31, 2017 Haha @harryh, and I haven't seen that mistake before. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now