• What's wrong with this (tmy)sql code?
    6 replies, posted
[lua] local lusionsql = require "tmysql" if not lusionsql then return else local Rp_DB local function LusionRp_SQLConnect() tmysql.initialize(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT) end hook.Add("Initialize", "lolfasdfsd", function( pl ) LusionRp_SQLConnect() end) function LusionRp_SQLSpawn( Player ) if ValidEntity( Player ) and Player:IsPlayer() then local query1 = tmysql.query("SELECT * FROM roleplaydata WHERE uniqueid='" .. Player:NSteamID() .. "'") if ( query1 ) then print("Loaded info successfully: " .. Player:RealName() .. " (" .. Player:SteamID() .. ")") else tmysql.query("INSERT INTO roleplaydata (uniqueid, lusionid, steamid, rpname, money, isarrested, hasphysgun, cansuicide) VALUES ('" .. Player:NSteamID() .. "', '" .. Player:GetNWInt("lusion_id") .. "', '" .. Player:SteamID() .. "', '" .. Player:GetNWString("Lusion_Nickname") .. "', '" .. GAMEMODE.config["Starting Wallet"] .. "', '" .. tostring(false) .. "', '" .. tostring(false) .. "', '" .. tostring(false) .. "')") print("Created info for: " .. Player:RealName() .. " (" .. Player:SteamID() .. ")") end end end -- Updating function LusionRp_UpdatePlayer( Player ) if ValidEntity( Player ) and Player:IsPlayer() then tmysql.query("INSERT INTO roleplaydata (uniqueid, lusionid, steamid, rpname, money, isarrested, hasphysgun, cansuicide) VALUES ('" .. Player:NSteamID() .. "', '" .. Player:GetNWInt("lusion_id") .. "', '" .. Player:SteamID() .. "', '" .. Player:GetNWString("Lusion_Nickname") .. "', '" .. Player:GetNWInt("currency") .. "', '" .. tostring(false) .. "', '" .. tostring(false) .. "', '" .. tostring(false) .. "')") -- tmysql.query("UPDATE roleplaydata SET money='" .. Player:GetNWInt("currency") .. "' WHERE uniqueID='" .. Player:NSteamID() .. "'") print("Attempted to update money for: " .. Player:RealName() .. " (" .. Player:SteamID() .. ")") end end function LusionRp_UpdateAll( Player ) if ValidEntity( Player ) and Player:IsPlayer() then tmysql.query("UPDATE roleplaydata SET money='" .. Player:GetNWInt("currency") .. "', rpname='" .. Player:GetNWString("Lusion_Nickname") .. "', hasphysgun='" .. tostring(Player:IsOwner()) .. "' WHERE uniqueid='" .. Player:NSteamID() .. "'") print("Attempted to update table for: " .. Player:RealName() .. " (" .. Player:SteamID() .. ")") end end -- Retrieving function LusionRp_GetMoney( Player ) if ValidEntity( Player ) and Player:IsPlayer() then local query1 = tmysql.query("SELECT money FROM roleplaydata WHERE uniqueid='" .. Player:NSteamID() .. "'") return query1 end end end print("Loading serverside sql.lua!") [/lua] This code (not exactly this code) worked before, but it was in combination with text files, sqlite, and tmysql - which was terrible. I managed to fix it so that players get stored once again, but now things aren't getting retrieved or saved. My money stays 0, even though I should have like 55,000 - or even 8,000 as default. It doesn't retrieve or save money, or anything else for that matter. I've been trying to fix this for days - I don't know what's wrong. Could it be something with me not including my sql file soon enough? Just ask if you need more code. If it helps this is the loading order of init.lua: [lua] include("configuration/sv_configuration.lua") include("configuration/sh_configuration.lua") include("sh_hooks.lua") include("sh_meta.lua") include("sv_meta.lua") include("shared.lua") include("util.lua") include("convars.lua") include("newvars.lua") include("sv_commands.lua") include("changelog.lua") AddCSLuaFile("configuration/sh_configuration.lua") AddCSLuaFile("cl_init.lua") AddCSLuaFile("sh_hooks.lua") AddCSLuaFile("rpskin.lua") AddCSLuaFile("shared.lua") AddCSLuaFile("cl_scoreboard.lua") AddCSLuaFile("util.lua") AddCSLuaFile("newvars.lua") include("sql.lua") -- test [/lua]
You can't connect on server initialize, a minimum of one player sock must be open when doing any queries.
[QUOTE=zzaacckk;34521127]You can't connect on server initialize[/QUOTE] Yes you can I have a script here that does exactly that, and it works fine
[QUOTE=my_hat_stinks;34525767]Yes you can I have a script here that does exactly that, and it works fine[/QUOTE] Yeah, when he posted that I was wondering because my script worked fine when it connected on initialize. Either way zzaacckk, the problem is still not fixed - but thanks for taking your time to help me. Anyways it appears that my code doesn't want to retrieve the sql data. It saves fine (well, for the most part) though. Slightly modified (still not working) sql.lua: [lua] local lusionsql = require "tmysql" if not lusionsql then return else local Rp_DB local function LusionRp_SQLConnect() tmysql.initialize(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT) end --hook.Add("PlayerInitialSpawn", "lolfasdfsd", function( pl ) hook.Add("Initialize", "lolfasdfsa", function( pl ) -- if #player.GetAll() > 0 then LusionRp_SQLConnect() --end end) function LusionRp_SQLSpawn( Player ) if ValidEntity( Player ) and Player:IsPlayer() then local query1 = tmysql.query("SELECT * FROM `roleplaydata` WHERE uniqueid='" .. Player:NSteamID() .. "'") if ( query1 ) then print("Loaded info successfully: " .. Player:RealName() .. " (" .. Player:SteamID() .. ")") else tmysql.query("INSERT INTO roleplaydata (uniqueid, lusionid, steamid, rpname, money, isarrested, hasphysgun, cansuicide) VALUES ('" .. Player:NSteamID() .. "', '" .. Player:GetNWInt("lusion_id") .. "', '" .. Player:SteamID() .. "', '" .. Player:GetNWString("Lusion_Nickname") .. "', '" .. GAMEMODE.config["Starting Wallet"] .. "', '" .. tostring(false) .. "', '" .. tostring(false) .. "', '" .. tostring(false) .. "')") print("Created info for: " .. Player:RealName() .. " (" .. Player:SteamID() .. ")") end end end -- Updating function LusionRp_UpdatePlayer( Player ) if ValidEntity( Player ) and Player:IsPlayer() then tmysql.query("INSERT INTO roleplaydata (uniqueid, lusionid, steamid, rpname, money, isarrested, hasphysgun, cansuicide) VALUES ('" .. Player:NSteamID() .. "', '" .. Player:GetNWInt("lusion_id") .. "', '" .. Player:SteamID() .. "', '" .. Player:GetNWString("Lusion_Nickname") .. "', '" .. Player:GetNWInt("currency") .. "', '" .. tostring(false) .. "', '" .. tostring(false) .. "', '" .. tostring(false) .. "')") -- tmysql.query("UPDATE roleplaydata SET money='" .. Player:GetNWInt("currency") .. "' WHERE uniqueID='" .. Player:NSteamID() .. "'") print("Attempted to update money for: " .. Player:RealName() .. " (" .. Player:SteamID() .. ")") end end function LusionRp_UpdateAll( Player ) if ValidEntity( Player ) and Player:IsPlayer() then tmysql.query("UPDATE `roleplaydata` SET lusionid='" .. Player:GetNWInt("lusion_id") .. "' WHERE uniqueid='" .. Player:NSteamID() .. "'") tmysql.query("UPDATE `roleplaydata` SET rpname='" .. Player:GetNWString("Lusion_Nickname") .. "' WHERE uniqueid='" .. Player:NSteamID() .. "'") tmysql.query("UPDATE `roleplaydata` SET money='" .. (Player:GetNWInt("currency") or 0) .. "' WHERE uniqueid='" .. Player:NSteamID() .. "'") tmysql.query("UPDATE `roleplaydata` SET isarrested='" .. tostring(false) .. "' WHERE uniqueid='" .. Player:NSteamID() .. "'") tmysql.query("UPDATE `roleplaydata` SET hasphysgun='" .. tostring(false) .. "' WHERE uniqueid='" .. Player:NSteamID() .. "'") tmysql.query("UPDATE `roleplaydata` SET cansuicide='" .. tostring(false) .. "' WHERE uniqueid='" .. Player:NSteamID() .. "'") end print("Attempted to update table for: " .. Player:RealName() .. " (" .. Player:SteamID() .. ")") end -- Retrieving function LusionRp_GetMoney( Player ) if ValidEntity( Player ) and Player:IsPlayer() then return tmysql.query("SELECT money FROM `roleplaydata` WHERE uniqueid='" .. Player:NSteamID() .. "'") end end end print("Loading serverside sql.lua!") [/lua] What's really aggravating though is that everything worked fine when I was retrieving and setting PData, but now everything fell apart.
[lua]return tmysql.query("SELECT money FROM `roleplaydata` WHERE uniqueid='" .. Player:NSteamID() .. "'")[/lua] I'm pretty sure you can't do that. I think you have to use the callback function, like so: [lua]tmysql.query("SELECT money FROM `roleplaydata` WHERE uniqueid='" .. Player:NSteamID() .. "'", function(result, status, error) if error and error != 0 then print("SQL ERROR: " .. error) return end // Do something with result end) [/lua]
[QUOTE=SammyServers;34531995][lua]return tmysql.query("SELECT money FROM `roleplaydata` WHERE uniqueid='" .. Player:NSteamID() .. "'")[/lua] I'm pretty sure you can't do that. I think you have to use the callback function, like so: [lua]tmysql.query("SELECT money FROM `roleplaydata` WHERE uniqueid='" .. Player:NSteamID() .. "'", function(result, status, error) if error and error != 0 then print("SQL ERROR: " .. error) return end // Do something with result end) [/lua][/QUOTE] Holy fucking shit. That would explain why my _R.Player:GetMoney() would return a nil value, and everything else involving money wouldn't work. Thank you sir! Yeah that would make sense since I was doing something similar to that in mysqloo.
[QUOTE=my_hat_stinks;34525767]Yes you can I have a script here that does exactly that, and it works fine[/QUOTE] Oh wow, I guess it was updated. Now I have to go optimize my code :[ again
Sorry, you need to Log In to post a reply to this thread.