• Mysql loading? problems custom code.
    3 replies, posted
So i'm looking for someone to help me fix or me to pay someone to help me fix my mysql loading problem i'm like 90% done with it like it save and all but the loading will just not work for some reason. [CODE]/* if (!SERVER) then return; end; require ("mysqloo") // Include the modules require ("glon") local DATABASE_HOST = "**************" local DATABASE_PORT = 3306 local DATABASE_NAME = "*********" local DATABASE_USERNAME = "***********" local DATABASE_PASSWORD = "*********************" local CONNECT = false; function file.AppendLine(filename, addme) data = file.Read(filename, "DATA") if ( data ) then file.Write(filename, data .. "\n" .. tostring(addme)) else file.Write(filename, tostring(addme)) end end function lordLog(str, fn) if not fn then fn = "lord_mysql.txt" end file.AppendLine(fn, "[Lord's Log: "..tostring(os.date()).."] "..str) end local function connectToDatabase2() INVENTORY_MYSQL = mysqloo.connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT) INVENTORY_MYSQL.onConnected = function() CONNECT = true; lordLog("Connected to Database."); checkTable2(INVENTORY_MYSQL); end INVENTORY_MYSQL.onConnectionFailed = function() lordLog("Failed to connect to the database.") CONNECT = false;end INVENTORY_MYSQL:connect(); lordLog("loading from mysql tell jason if you see this message5"); MsgN("CONNECTED"); //CONNECT = true; end connectToDatabase2(); function checkQuery2(query) local playerInfo = query:getData() if playerInfo[1] ~= nil then return true else return false end end function checkTable2(server) print("Create MYSQL Pdata.") local ii = ','; local str = ''; for i = 1, 5 do str = str .. ' equipslot'..tostring(i)..' TEXT' .. ii; end for i = 1, 50 do if (i == 50) then ii = ''; end; str = str .. ' slot'..tostring(i)..' TEXT'..ii end local Qc = server:query( "CREATE TABLE IF NOT EXISTS playerinventories ( steamid VARCHAR(255) NOT NULL PRIMARY KEY,"..str.. ");") Qc.onError = function(Q,Err) print("Failed to Create Table: " .. Err) end Qc:start() end local meta = FindMetaTable("Player"); --[[--------------------------------------------------------- GetPData - Saves persist data for this player self:GetPDataMYSQL("Inventory", nil, function(val, args) Args being a table full of arguments for the function. -----------------------------------------------------------]] function meta:GetPDataMYSQL( name, default, callback, args ) if (!CONNECT) then return; end; if (!IsValid(self)) then return; end; name = Format( "%s[%s]", self:UniqueID(), name ) local query1 = INVENTORY_MYSQL:query("SELECT value FROM playerinventories WHERE steamid = '" .. tostring(name) .. "'");// LIMIT 1");--" local val1 = nil; query1.onSuccess = function(q) if (checkQuery2(q) == true) then //self:ChatPrint("Loading from MYSQL"); val1 = q:getData(); callback(val1, default, args, false); else //callback(sql.QueryValue( "SELECT value FROM playerpdata WHERE steamid = " .. SQLStr(name) .. " LIMIT 1" ), default, args, true); end end query1:start(); return true; end function meta:LoadInventoryFromMYSQL() if (CONNECT) then if (!IsValid(self)) then return; end; local query1 = INVENTORY_MYSQL:query("SELECT * FROM playerinventories WHERE steamid = '" .. self:SteamID() .. "';")// LIMIT 1 local val1 = nil; query1.onSuccess = function(q) if (checkQuery2(q) == true) then --yes we get here val1 = q:getData(); for k, v in pairs(val1[1]) do self:ChatPrint(tostring(k).." !"..tostring(v).."!\n") end for k, v in pairs(val1[1]) do if (k != "steamid" and string.sub(k, 1, 4) == 'slot') then --I think this is working. But v is not printing anything? local tbl2 = {}; if (v) then local tbl = glon.decode(v) tbl2 = tbl; if (tbl2 and tbl2.INV_SLOT) then self:ChatPrint("Slot "..tostring(tbl2.INV_SLOT).." loaded"); self.LOOT_INVENTORY[tbl2.INV_SLOT] = tbl2; CheckMaxMods(tbl2, tbl2.QUALITY, tbl2.MODS); end end elseif (string.sub(k, 1, 5) == "equip") then local tbl = glon.decode(v); tbl2 = tbl; if (tbl2 and tbl2.EQ_SLOT) then self.LOOT_EQUIPED[tbl2.EQ_SLOT].ITEM = tbl2; CheckMaxMods(tbl2, tbl2.QUALITY, tbl2.MODS); end end end print("Sending inventory."); self.LOADED_INV = true; timer.Simple(5, function() print("TImer send inv.."); self:VerifyAndSendInv(true); end) else self:ChatPrint("Yo not loading right."); self:LoadInvDef(); end end query1.onError = function(q, err) file.AppendLine("MYSQL_ERRORS.txt", "Loading Error for :" .. self:Nick() .. " -- \n"..err ) self:ChatPrint("Loading from MYSQL failed, now loading from SQLLite, Report this, Load Error:" .. err); self:LoadInvDef(); end query1:start(); else return false; end return true; end function meta:SaveInventoryToMYSQL() if (!IsValid(self)) then return; end; if (!CONNECT) then self:ChatPrint("Not connected to MYSQL"); end self:ChatPrint("Saving yo inv to the database homie") local name = self:SteamID(); local str1 = ''; local str2 =''; local str3 = ''; local ii = ','; for i = 1, table.Count(self.LOOT_EQUIPED) do str3 = str3 ..' equipslot'..tostring(i)..' = VALUES(equipslot'..tostring(i)..')'..ii; str1 = str1 .. ' equipslot'..tostring(i)..ii; local tb = {} local s = self.LOOT_EQUIPED[i].ITEM; local tb = s; if (tb and tb.PrintName) then tb.PrintName = string.Replace(tb.PrintName, "Rush's SG Crutchmando", "SG Commando"); tb.PrintName = string.Replace(tb.PrintName, "Tyto's Best Friend", "Deagle"); end local iii = glon.encode(tb); str2 = str2 .." '".. iii.."'" .. ii; end for i = 1, self:GetNetworkedInt("MAX_INV_SLOTS") do if (i == 50) then ii = ''; end; str1 = str1 .. ' slot'..tostring(i)..ii; local tb = self.LOOT_INVENTORY[i]; --[[if (tb and tb.PrintName) then tb.PrintName = string.Replace(tb.PrintName, "Rush's SG Crutchmando", "SG Commando"); tb.PrintName = string.Replace(tb.PrintName, "Tyto's Best Friend", "Deagle"); end]] local iii = glon.encode(tb); str2 = str2 .." '".. iii.."'" .. ii; str3 = str3..' slot'..tostring(i)..' = VALUES(slot'..tostring(i)..')'..ii; end file.Write("MYSQLLOG.txt", tostring("INSERT INTO playerinventories ( steamid," ..str1.. ") VALUES ( '"..name.."', "..str2.." ) ON DUPLICATE KEY UPDATE "..str3)) local query1 = INVENTORY_MYSQL:query( "INSERT INTO playerinventories ( steamid," ..str1.. ") VALUES ( '"..name.."', "..str2.." ) ON DUPLICATE KEY UPDATE "..str3); query1.onError = function(Q,Err) self:ChatPrint("Failed to save to MYSQL Please report this: " .. Err) file.AppendLine("MYSQL_ERRORS.txt", "Save Error for :" .. self:Nick() .. " -- \n "..err ) end query1.onSuccess = function(q) if (q) then if (checkQuery2(q) == true) then if (IsValid(self)) then end lordLog("Saved Inventory data for "..self:Nick().." to Mysql"); else if (IsValid(self)) then //self:ChatPrint("Saved inventory data to MYSQL"); end end end end query1:start() end function meta:SetPDataMYSQL( name, value ) //print("TRYING TO QUERY"); if (!IsValid(self)) then return; end; name = Format( "%s[%s]", self:UniqueID(), name ) if (value == nil) then value = tostring(value) end; local query1 = INVENTORY_MYSQL:query( "REPLACE INTO playerinventories ( steamid, value ) VALUES ( "..SQLStr(name)..", '"..value.."' )"); query1.onError = function(Q,Err) self:print("Failed to save PData " .. Err) end query1.onSuccess = function(q) if (q) then if (checkQuery2(q) == true) then print("Saved Inventory data to Mys
Good god. Any chance you could show the error or particular segment of code that is actually not working? [editline]13th July 2014[/editline] Also you should be escaping things.
This is the loading part of the code i'm having its just not work no error. [Code] function meta:LoadInventoryFromMYSQL() if (CONNECT) then if (!IsValid(self)) then return; end; local query1 = INVENTORY_MYSQL:query("SELECT * FROM playerinventories WHERE steamid = '" .. self:SteamID() .. "';")// LIMIT 1 local val1 = nil; query1.onSuccess = function(q) if (checkQuery2(q) == true) then --yes we get here val1 = q:getData(); for k, v in pairs(val1[1]) do self:ChatPrint(tostring(k).." !"..tostring(v).."!\n") end for k, v in pairs(val1[1]) do if (k != "steamid" and string.sub(k, 1, 4) == 'slot') then --I think this is working. But v is not printing anything? local tbl2 = {}; if (v) then local tbl = glon.decode(v) tbl2 = tbl; if (tbl2 and tbl2.INV_SLOT) then self:ChatPrint("Slot "..tostring(tbl2.INV_SLOT).." loaded"); self.LOOT_INVENTORY[tbl2.INV_SLOT] = tbl2; CheckMaxMods(tbl2, tbl2.QUALITY, tbl2.MODS); end end elseif (string.sub(k, 1, 5) == "equip") then local tbl = glon.decode(v); tbl2 = tbl; if (tbl2 and tbl2.EQ_SLOT) then self.LOOT_EQUIPED[tbl2.EQ_SLOT].ITEM = tbl2; CheckMaxMods(tbl2, tbl2.QUALITY, tbl2.MODS); end end end print("Sending inventory."); self.LOADED_INV = true; timer.Simple(5, function() print("TImer send inv.."); self:VerifyAndSendInv(true); end) else self:ChatPrint("Yo not loading right."); self:LoadInvDef(); end end query1.onError = function(q, err) file.AppendLine("MYSQL_ERRORS.txt", "Loading Error for :" .. self:Nick() .. " -- \n"..err ) self:ChatPrint("Loading from MYSQL failed, now loading from SQLLite, Report this, Load Error:" .. err); self:LoadInvDef(); end query1:start(); else return false; end return true; end[/Code]
We need more to go on than a chunk of code. Here are a few db related tutorials; mainly just connecting... [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/database/_connecting_with_fallbacks_in_place.lua.html[/url] [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/database/_connecting_with_mysqloo.lua.html[/url] [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/database/_connecting_with_tmysql.lua.html[/url] Tut just goes over several simple types of queries and the format... [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/database/basic_queries_and_query_formats_used_to_check_for_existing_row.lua.html[/url] And something you may be interested in. Converting playerpdata to MySQL insert queries: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/database/converting_sv_db_to_mysql.lua.html[/url] Edit: I'd recommend adding print statements throughout the code; it is one of the easiest ways, and most overlooked way, of debugging code. See what executes, where it hangs up, etc...
Sorry, you need to Log In to post a reply to this thread.