Lua SQL Error

Hi, i’m trying to make a code that loads time on player join through a sql database, but i keep getting a really anoying error:


autorun/sv.utime:45: attempt to index field '?' <a nil value>

I know whats causing the error (res[1][1]) If the player has a value in the database then it returns their SteamID but if theres no record it returns that error, which is causing the whole script to not to work. I dont need to use this code i just need a code that says ‘If SteamID does exist, Load Account else Create Account’

Heres the actual script:
[lua]
if not SERVER then return end

require( “tmysql” )

local host = “"
local username = "

local password = “"
local database = "

local port = 3306
local connections = 6
local threads = 5

function LoadSQLConnection()
Msg("==================================================================
“);
Msg(”==================== Initializing SQL Connection =================
“);
Msg(”==================================================================
")

tmysql.initialize(host, username, password, database, port, connections, threads)

function Query(...)
Msg("=== Query Executing === 

")
return tmysql.query(…)
end

function Esc(esc)
return tmysql.escape(esc)
end

function QueryErrors(res,status,error)
Error( "Error: " ..error.. "

")
end

Msg("========== Connection To SAS SQL Database Established! ===========

“);
Msg(”==================================================================
")
end

function LoadAccount( ply )
local steamid = Esc(ply:SteamID())
local nick = Esc(ply:Nick())

Query("SELECT * FROM sasserver WHERE SteamID = \'"..tostring(steamid).."\'", function(res,status,error)
if !status then Error( "Error: " ..error.. "

“) end
local timer = 0
if ( res[1][1] == steamid ) then
timer = res[1][3]
ply:PrintMessage( HUD_PRINTTALK, “Loaded “…tostring(nick)…”'s SAS Profile!” )
end
if (res[1][1] != steamid ) then
timer = 0
Query(“INSERT INTO sasserver ( SteamID, nick, time ) VALUES ( '”…tostring(steamid)…”’, ‘"…tostring(nick)…"’, ‘"…tostring(timer)…"’ )", function(result,succ,error2)
if !succ then Error( "Error: " …error2… "
“) end
ply:PrintMessage( HUD_PRINTTALK, “Creating SAS Profile For " …tostring(nick)…”!” )
end )
end
ply:SetUTime( timer )
ply:SetUTimeStart( CurTime() )
end )
end

function SaveAccount( ply )
local steamid = Esc(ply:SteamID())
local nick = Esc(ply:Nick())
local utime = math.floor(ply:GetUTimeTotalTime())
Query(“UPDATE sasserver SET nick = '”…tostring(nick)…"’, time = ‘"…tostring(utime)…"’ WHERE SteamID = ‘"…tostring(steamid)…"’")
end

function SaveAll()
local players = player.GetAll()

for _, ply in ipairs( players ) do
	if ply and ply:IsConnected() then
		SaveAccount( ply )	
	end
end

end

timer.Create( “UTimeSaver”, 60, 0, SaveAll )

hook.Add( “InitPostEntity”, “SASSQLConnect”, LoadSQLConnection )
hook.Add( “PlayerInitialSpawn”, “SASPlayerSpawn”, LoadAccount )
hook.Add( “PlayerDisconnected”, “SASPlayerDisconnect”, SaveAccount )
[/lua]

Are you sure you’re accessing the table the right way? Shouldn’t the SteamID be just res[1]?

If you’re unsure do PrintTable(res)

Oh and also you’re only accessing the the first entry? And your saving code doesn’t seem very consistent. You seem to be using a different structure for loading and updating. May I suggest you use the steamID to index entries, and then associate the name and time to it?

So your condition could be resumed as : [lua]for k,v in pairs(res) do
if ( k == steamid ) then
timer = v[2]
ply:PrintMessage( HUD_PRINTTALK, “Loaded “…tostring(nick)…”'s SAS Profile!” )
else
timer = 0
Query(“INSERT INTO sasserver ( SteamID, nick, time ) VALUES ( '”…tostring(steamid)…"’, ‘"…tostring(nick)…"’, ‘"…tostring(timer)…"’ )", function(result,succ,error2) //Honestly not sure about this
if !succ then Error( "Error: " …error2… "
“) end
ply:PrintMessage( HUD_PRINTTALK, “Creating SAS Profile For " …tostring(nick)…”!” )
end )
end
end[/lua]

Thanks ill test it out, also as for the res[1] thing, if there is a steamid in the database res[1][1] will return the steamid, where as res[1] will return a string of numbers totaly unacosiated with steamid, but if no steamid was found in the database both res[1] and res[1][1] will return errors.

Is that because the table is empty? That’s why you must not specify a key. Oh and the code I posted was dumb.

[lua]
local timer = 0
for k,v in pairs(res) do
if ( v[1] == steamid ) then
timer = v[3]
ply:PrintMessage( HUD_PRINTTALK, “Loaded “…tostring(nick)…”'s SAS Profile!” )
end
end
if timer == 0 then
Query(“INSERT INTO sasserver ( SteamID, nick, time ) VALUES ( '”…tostring(steamid)…"’, ‘"…tostring(nick)…"’, ‘"…tostring(timer)…"’ )", function(result,succ,error2) //Honestly not sure about this
if !succ then Error( "Error: " …error2… "
“) end
ply:PrintMessage( HUD_PRINTTALK, “Creating SAS Profile For " …tostring(nick)…”!” )
end
end[/lua]

So you check all of they keys for a matching steamID then if none were found you create the record.

It woooookredddd! Thanks!