• Telling if a SQL query returned nothing using tmysql
    9 replies, posted
How can i tell if my query returned nothing? like, i wana have it so if the player isnt i the databse, then add them. But if they are, then just print the database info on them. But it is never adding them, i'v tryed to do !result or not result or result == nill etc. but its always acting like there IS data there, but there isnt. Here's the code, and i posted this here because its not a newbie question :D [lua] require("tmysql") tmysql.initialize("localhost", "root", "mpass", "gmod_users", 3306, 1, 1) function callbackfunc(result, status, error) Msg("\nCallback function is now being run.\n") Msg("Query result:\n\n") if result then PrintTable(result) Msg("\n Status: "..status.."\n Error: "..error.."\n") else forkvin() end end function query(strquery) Msg("Query:\n "..strquery.."\n is now being run.") tmysql.query(strquery, callbackfunc) end for k, v in pairs(player.GetAll()) do Msg("Player name: "..v:Nick().."\n Player steamid: "..v:SteamID().."\n") local strquery = "SELECT * FROM gmod_class WHERE steamid = '"..v:SteamID().."'" query(strquery) end function forkvin() Msg("\n Forkvin being run now.") for k, v in pairs(player.GetAll()) do local strquery = "INSERT INTO gmod_class (steamid, class) VALUES( '"..v:SteamID().."', 'guest')" Msg("Query:\n"..strquery.."\n") query(strquery) end end [/lua] [editline]10:24AM[/editline] The code's a bit messy :D
Use Result[1] The first entry is always the row(if there isn't multiple) returned.
Ok, so if !result[1]? [editline]10:36AM[/editline] or if result[1] == 0 or nill or something?
Can't you simply check if result == {} then?
hmmm, thats an idea. :D thanks. [editline]10:57AM[/editline] Nope. Still same exact thing [lua] require("tmysql") tmysql.initialize("localhost", "root", "a password", "gmod_users", 3306, 1, 1) function forkvin() Msg("\n Forkvin being run now.") for k, v in pairs(player.GetAll()) do local strquery = "INSERT INTO gmod_class (steamid, class) VALUES( '"..v:SteamID().."', 'guest')" Msg("Query:\n"..strquery.."\n") query(strquery) end end function callbackfunc(result, status, error) Msg("\nCallback function is now being run.\n") Msg("Query result:\n\n") if result[1] == 0 then forkvin() else PrintTable(result) Msg("\n Status: "..status.."\n Error: "..error.."\n") end end function query(strquery) Msg("Query:\n "..strquery.."\n is now being run.") tmysql.query(strquery, callbackfunc) end for k, v in pairs(player.GetAll()) do Msg("Player name: "..v:Nick().."\n Player steamid: "..v:SteamID().."\n") local strquery = "SELECT * FROM gmod_class WHERE steamid = '"..v:SteamID().."'" query(strquery) end [/lua] and the console output is [code] Player name: x Player steamid: a steamid Query: SELECT * FROM gmod_class WHERE steamid = 'a steamid' is now being run. Callback function is now being run. Query result: Status: 1 Error: 0 [/code] [editline]10:58AM[/editline] i also tryed result == {} and i got the same thing again
if type( result ) == "table" then
[QUOTE=Kogitsune;16834196]if type( result ) == "table" then[/QUOTE] Regardless if Data is found or not, it'll always return a table Use [lua] if !result[1] then //Insert stuff end [/lua]
[QUOTE=bennyg;16834241]Regardless if Data is found or not, it'll always return a table[/QUOTE] Considering that if tblX == { } then Should never run the code, I think my reply was correct :V:. It wasn't the solution to the thread, but rather a correction of the way to determine of the object is a table.
It worked too well... Showing rows 0 - 29 (4,650 total, Query took 0.0004 sec) Ummm... it ran 4,650 times before i stoped it... [editline]11:19AM[/editline] How can i fix this? [editline]11:36AM[/editline] Ok, i made it not do that loop thing, but now its never going to the callbacfunc: [lua] require("tmysql") tmysql.initialize("localhost", "root", "nevar", "gmod_users", 3306, 1, 1) function forkvin() Msg("\n Forkvin being run now.") for k, v in pairs(player.GetAll()) do local strquery = "INSERT INTO gmod_class (steamid, class) VALUES( '"..v:SteamID().."', 'guest')" Msg("Query:\n"..strquery.."\n") query(strquery) init() end end function callbackfunc(result, status, error) Msg("\nCallback function is now being run.\n") Msg("Query result:\n\n") if !result[1] then forkvin() else PrintTable(result) Msg("\n Status: "..status.."\n Error: "..error.."\n") end end function query(strquery) Msg("Query:\n "..strquery.."\n is now being run.") tmysql.query(strquery, callbackfunc) end function init() for k, v in pairs(player.GetAll()) do Msg("Player name: "..v:Nick().."\n Player steamid: "..v:SteamID().."\n") local strquery = "SELECT * FROM gmod_class WHERE steamid = '"..v:SteamID().."'" query(strquery) end end concommand.Add( "Initmysql", init) [/lua] And, as i said, its never going to the callback func, its just stopping at the query. [code] Player name: my name Player steamid: some steamid Query: SELECT * FROM gmod_class WHERE steamid = 'mah steamid' is now being run. [/code] [editline]11:48AM[/editline] What can i do about this?
Count the result table. #result. The game doesn't process Think hooks unless there is a player in game, so on a dedicated server you need to add a bot or something to wake it up.
Sorry, you need to Log In to post a reply to this thread.