• MySQL Problem
    4 replies, posted
[lua] RP.DB.Query("SELECT * FROM `rp_players`",function (data) if data == nil then return end print(data.sid .. " " .. "STEAM_0:0:0") if data.sid == "STEAM_0:0:0" then found = true end end)[/lua] I'm trying to get it to insert a new player if one does not exist already, although I keep getting errors when trying to access data, even though it does not class as nil. According to type, it's a table, but it won't allow me to use PrintTable.
Dont do it like that. [lua] local query = "SELECT Count(*) FROM rp_players WHERE steamid = '"..data.sid.."'"; RP.DB.Query(query,function(data) if((data['Count(*)'] or data[1])<1)then print("SteamID "..data.sid.." has a row in rp_players!"); end end) [/lua]
Data is always returned in a table, use [1][1]
I got it working, but then it's started doing it again for no reason, here's my whole code: [lua] function RP.DB.InitPlayer( ply ) if RP.DB.ConnectionValid == false then error("Cannot query database, connection not open!") return end RP.DB.Query("SELECT * FROM `rp_players`", function (data) PrintTable(data) for k, v in pairs(data) do if v.sid == ply:SteamID() then RP.Vars.UpdateVar(ply, "WalletStyle", v.wallet) RP.Vars.UpdateVar(ply, "Money", v.money) if v.vip == 1 then RP.Vars.UpdateVar(ply, "IsVIP", true) RP.Vars.UpdateVar(ply, "Tag", v.tagline) ply:SetNetworkedString(v.tagline) else RP.Vars.UpdateVar(ply, "IsVIP", false) end return end end end) print("Need to insert: " .. ply:SteamID()) RP.DB.Query("INSERT INTO `gmrp`.`rp_players` (`sid` ,`wallet` ,`money`, `vip) VALUES ('".. ply:SteamID() .. "', '" .. 1 .. "', '" .. RP.Config["StartingMoney"] .. "', '0');") RP.DB.InitPlayer( ply ) end [/lua] The strange thing is, I can see the vars client. So it's sent it once successfully, but it seems to be trying more than once. [editline]16th July 2011[/editline] Nevermind, fixed
That code is terrible. Once about 2-3 thousand players join and you query that and loop through it, its not gonna be good for the server.
Sorry, you need to Log In to post a reply to this thread.