• Msqloo make a player entry
    4 replies, posted
It connects fine just i want to check if a player has an entry then make one given they do not. It all works but i dont know how to sent the player thru to the getUserData function. How do i get the player i wanted to run the query for. [lua]require("mysqloo") //--Connect to database local DATABASE_HOST = "****" local DATABASE_NAME = "****" local DATABASE_USERNAME = "****" local DATABASE_PASSWORD = "****" local databaseObject = nil local database = nil function Connect() databaseObject = mysqloo.connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, 3306) databaseObject.onConnected = function(datab) database = datab end databaseObject:connect() end Connect() function getUserData(i) D = i:getData()[1] if(D == nil)then //RunQuery("INSERT INTO players ( Steam, Name, Points, Status) VALUES ( 1, '2', 0, 0)") RunQuery("INSERT INTO players ( Steam, Name, Points, Status) VALUES ( "..ply:UniqueID()..", '"..ply:Nick().."', 0, 0)") else end end function RunQuery(q) local query1 = database:query(q) query1:start() end function FirstSpawn( ply ) local query1 = database:query("SELECT * FROM players WHERE Steam = "..ply:UniqueID()) query1.onSuccess = getUserData query1:start() end hook.Add( "PlayerInitialSpawn", "mhsPointsInit", FirstSpawn )[/lua]
put getuserdata into the "Player" metatable. thats how i do it anyway. ex: [lua] local meta = FindMetaTable("Player"); function meta:getUserData(i) D = i:getData()[1] if(D == nil)then //RunQuery("INSERT INTO players ( Steam, Name, Points, Status) VALUES ( 1, '2', 0, 0)") RunQuery("INSERT INTO players ( Steam, Name, Points, Status) VALUES ( "..self:UniqueID()..", '"..self:Nick().."', 0, 0)") else end end[/lua] then you can call it using ply:getUserData(i); in another part of the gamemode/addon
[QUOTE=Silv;30615238]put getuserdata into the "Player" metatable. thats how i do it anyway. ex: [lua] local meta = FindMetaTable("Player"); function meta:getUserData(i) D = i:getData()[1] if(D == nil)then //RunQuery("INSERT INTO players ( Steam, Name, Points, Status) VALUES ( 1, '2', 0, 0)") RunQuery("INSERT INTO players ( Steam, Name, Points, Status) VALUES ( "..self:UniqueID()..", '"..self:Nick().."', 0, 0)") else end end[/lua] then you can call it using ply:getUserData(i); in another part of the gamemode/addon[/QUOTE] iv never used the meta table before and dont quite understand it. Could you give me a mini example. What im getting is this adds the function to the player so i can call it ply:getUserData(i) or so? see the issue is i have to use query1.onSuccess = getUserData and i dont know how to pass the player to that function
Allow me to be of assistance with what a Metatable can be used for. You can find more information on MetaTables provided here: [url]http://wiki.garrysmod.com/?title=G.FindMetaTable[/url] In this case, I will suggest the Player's MetaTable. [lua] local Player = FindMetaTable("Player") -- Here we can grab the players MetaTable --Why is this useful? Because, we can add values to the table, such as functions and variables. --Say we want to infect a player with a virus. Well here we can add and check an infection variable. local Player.Infected = false --In this case, we can set the variable to false for the player being infected. if (Player.Infected == true) then --Checking to see if the player is sick, if so then Player:SetColor(Color(0,0,0,255)) --Set the color of the player to Black. end --If not, do nothing. [/lua]
HELP! i modified like this , but it seems to be not work [lua] --[[ MySQL PData Replacement by _Undefined - Used for sharing player data between servers. ]]-- include("mysqlpdata_config.lua") require("mysqloo") -- Error handler. local function DBError(self, err) if err then MsgN("PData [DB Error]: " .. err) end end -- Connect. local PDataDB = mysqloo.connect(pdatadb_host, pdatadb_user, pdatadb_pass, pdatadb_name, pdatadb_port) PDataDB.onConnectionFailed = DBError PDataDB:connect() -- If no connection, stop here. if not PDataDB:status() == mysqloo.DATABASE_CONNECTED then MsgN("PData [DB]: Connection failed! (" .. err .. ")") return end local Player = _R.Player local oldsetpdata = _R.Player.SetPData -- Only need to override Set, as Get will always pull from the local database, and be synched. -- Override SetPData to send to MySQL. function Player:SetPData(key, value) oldsetpdata(self, key, value) -- Keep it local. MsgN("PData [SQL -> MySQL]: Updating '" .. key .. "' for '" .. self:UniqueID() .. "' with '" .. value .. "'!") deleteQ = PDataDB:query("DELETE FROM `playerdata` WHERE `uniqueid` = '" .. self:UniqueID() .. "' AND `key` = '" .. key .. "'") deleteQ.onError = DBError deleteQ:start() insertQ = PDataDB:query("INSERT INTO `playerdata` (`uniqueid`, `key`, `value`) VALUES ('" .. self:UniqueID() .. "', '" .. key .. "', '" .. value .. "')") insertQ.onError = DBError insertQ:start() end -- Sync from MySQL to local SQL. Done on a timer, or manually with pdata_mysql2sql. local function MySQL2SQL() selectQ = PDataDB:query("SELECT * FROM `playerdata`") selectQ.onData = function(self, data) MsgN("PData [MySQL -> SQL]: Updated '" .. data.key .. "' for '" .. data.uniqueid .. "' with '" .. data.value .. "'!") key = Format("%s[%s]", data.uniqueid, data.key) sql.Query("DELETE FROM playerpdata WHERE infoid = " .. SQLStr(data.key)) sql.Query("INSERT INTO playerpdata (infoid, value) VALUES ("..SQLStr(data.key)..", "..SQLStr(data.value)..")") end selectQ.onError = DBError selectQ:start() end -- Sync from local SQL to MySQL. Only done by command, and should be done once on the ititial server. local function SQL2MySQL() local data = sql.Query("select * from playerpdata") for _, row in pairs(data) do local value = row['value'] local infoid = row['infoid'] local split = string.Explode("[", infoid) local uniqueid = split[1] local key = string.sub(split[2], 1, -2) deleteQ = PDataDB:query("DELETE FROM `playerdata` WHERE `uniqueid` = '" .. uniqueid .. "' and `key` = '" .. key .. "'") deleteQ.onError = DBError deleteQ:start() insertQ = PDataDB:query("INSERT INTO `playerdata` (`uniqueid`, `key`, `value`) VALUES ('" .. uniqueid .. "', '" .. key .. "', '" .. value .. "')") insertQ.onError = DBError insertQ:start() MsgN("PData [SQL -> MySQL]: Updated '" .. key .. "' for '" .. uniqueid .. "' with '" .. value .. "'!") end end -- Create timer to update SQL from MySQL. timer.Create("Update_Local_PData", pdatasync_schedule, 0, function() MySQL2SQL() end) -- Command to sync from SQL to MySQL. concommand.Add("pdata_sql2mysql", function(ply, cmd, args) if not ply:IsSuperAdmin() then return end if not ply.HasBeenWarned then ply.HasBeenWarned = true ply:PrintMessage(HUD_PRINTCONSOLE, "WARNING! This command will overwrite anything in the MySQL database with the data in the local SQL database. Are you sure you want to do this? Run this command again to confirm.\n") return else ply:PrintMessage(HUD_PRINTCONSOLE, "Updated MySQL with SQL data!\n") SQL2MySQL() end end) -- Command to sync from MySQL to SQL. Should be run once on initial server. concommand.Add("pdata_mysql2sql", function(ply, cmd, args) if not ply:IsSuperAdmin() then return end if not ply.HasBeenWarned then ply.HasBeenWarned = true ply:PrintMessage(HUD_PRINTCONSOLE, "WARNING! This command will overwrite anything in the local SQL database with data from the MySQL database. Are you sure you want to do this? Run this command again to confirm.\n") return else ply:PrintMessage(HUD_PRINTCONSOLE, "Updated SQL with MySQL data!\n") MySQL2SQL() end end) function getUserData(i) local meta = FindMetaTable("Player"); function meta:getUserData(i) D = i:getData()[1] if(D == nil)then //RunQuery("INSERT INTO players ( Steam, Name, Points, Status) VALUES ( 1, '2', 0, 0)") RunQuery("INSERT INTO players ( Steam, Name, Points, Status) VALUES ( "..self:UniqueID()..", '"..self:Nick().."', 0, 0)") else end end end function RunQuery(q) local query1 = database:query(q) query1:start() end function FirstSpawn( ply ) local query1 = database:query("SELECT * FROM players WHERE Steam = "..ply:UniqueID()) query1.onSuccess = getUserData query1:start() end hook.Add( "PlayerInitialSpawn", "mhsPointsInit", FirstSpawn ) [/lua] In mysql. [img]http://blogfiles.naver.net/20110708_210/cyw960517_1310115735990zCk9x_JPEG/2011-07-08_18%3B01%3B43.jpg[/img] Please help me!! :(
Sorry, you need to Log In to post a reply to this thread.