Hello, this is my only scrip on the Server for testing purposes.
My Problem is, when i connect to the Server and Press F1 firstly (calls function showHelp), it shows me an error. But when i call the function twice, the ResultSet is successfully transfered to the server. I am struggling around with this for a few day now, and i have no clue how to fix it.
Im
Code: (The establishconnection call Function in showHelp does not make it better)
require ("mysqloo")
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
function establishConnection ()
db = mysqloo.connect("localhost", "root", "", "duckRP", 3306)
db.onConnectet = function() print ("Esatblished Conenction") end
db.onConnectionFailed = function (db, err) print(err) end
db:connect()
end
local result
function localquery (db, query)
local q = db:query(query)
q.onSuccess = function(q, data)
local ret = q:getData()
result = ret
end
q.onError = function(q, err, sql) print(err) end
q:start()
return result
end
establishConnection()
function GM:ShowHelp(player)
establishConnection()
local a = localquery(db, "select * from playerdata")
PrintTable(a)
end
function GM:ShowTeam(player)
local a = localquery(db, "update playerdata set rpMoney = 10 where steamName = 'Duckfine'")
PrintTable(a)
end
Error (First Press and then the Successfully transfered Data.) (console)
[ERROR] lua/includes/extensions/table.lua:679: bad argument #1 to 'pairs' (table expected, got nil)
1. pairs - [C]:-1
2. GetKeys - lua/includes/extensions/table.lua:679
3. PrintTable - lua/includes/util.lua:62
4. unknown - gamemodes/mysql/gamemode/init.lua:33
1:
playerID = 2
rpCusInventory = {"name":"data"}
rpExpPoints = 0
rpLevel = 1
rpMainInventory = []
rpMoney = 10
rpName = n/a
steamID64 = 7.6561198089289e+16
steamName = Duckfine
Screaming for help...
In most cases you won't instantly get results from the database so the function localquery will return nil.
Hmm okay..? Means that this is a bug? How may i fix it?
You can fix this by supplying a callback function to run whatever you need when the query finishes. If you don't want to do this, you can halt the server while it fetches data but it isn't recommended if your connection to the database is poor.
Another option if you want non callback hungry code is to use coroutines.
What do you mean with "callback function"? How may i do that?
Sorry, you need to Log In to post a reply to this thread.