• Sql query not returning a table?
    6 replies, posted
I have this code here, and when i do sql.Query("SELECTblah blah") it should return a table... right? it returns a boolean. Why? Can someone help me? Thanks! [lua] function RetTeam(ply,Table) query = "SELECT group(*) AS grp FROM "..Table.." WHERE uid = '"..ply:SteamID().."'" result = sql.Query(query) Msg(tostring(result).."\n") for id, val in pairs(result) do res = tostring(val['grp']) end Msg("Query is of value:\n") Msg(res.."\n") if(result) then Msg("Player: "..ply:Nick().." belongs in team: "..res.."\n") return res else Msg("Player is not in the table. Query:\n") Msg(query.."\n") return false end end [/lua] oh right, heres the error [code] ERROR: GAMEMODE:'PlayerSpawn' Failed: Testing/gamemode/init.lua:105: bad argument #1 to 'pairs' (table expected, got boolean) [/code]
Try using [url=http://wiki.garrysmod.com/?title=Sql.QueryValue]sql.QueryValue[/url] instead of just sql.Query.
I tryed that first, hold on, let me get you the error. [editline]01:34PM[/editline] Sorry, it doesnt give an error. But QueryValue returns a boolean. look, here's the debug: [code] Table plyinfo Exists! Player Energy. Exists! QueryValue is of value: false Player is not in the table. Query: SELECT group FROM plyinfo WHERE uid = 'UNKNOWN' [/code] and when i enable this part: [lua] function TeamDecode(letter) Teams = {} Teams["R"] = 2 Teams["A"] = 4 Teams["S"] = 5 Teams["O"] = 1 Team = Teams[letter] --Msg("Team "..letter.." decoded to team number: "..Team) return Team end [/lua] when i uncomment out the Msg part i get [code] ERROR: GAMEMODE:'PlayerSpawn' Failed: Testing/gamemode/init.lua:122: attempt to concatenate global 'Team' (a nil value) [/code] and [lua] Msg("Team "..letter.." decoded to team number: "..Team) [/lua] IS on line 122
Umm, your problem is you're trying to run this from Singleplayer. You can't access your SteamID in Singleplayer. Run a listen server and try again.
But shouldnt it still work, even if the steamid is UNKNOWN? its still a VARCHAR [editline]01:46PM[/editline] Ok, so its in a listen server. QueryValue is still returning false. debug: [code] QueryValue is of value: false Player is not in the table. Query: SELECT group FROM plyinfo WHERE uid = 'Valid steamid here' Timer Error: Testing/gamemode/init.lua:124: attempt to concatenate global 'Team' (a nil value) [/code] I added a timer to go around the STEAMID_PENDING thing [editline]01:47PM[/editline] Again, here is the code with line 124 in it: [lua] function TeamDecode(letter) Teams = {} Teams["R"] = 2 Teams["A"] = 4 Teams["S"] = 5 Teams["O"] = 1 Team = Teams[letter] Msg("Team "..letter.." decoded to team number: "..Team) -- line 124 return Team end [/lua] I found this... how can i get around it? [code] ] lua_run PrintTable(sql.Query("SELECT * FROM plyinfo")) > PrintTable(sql.Query("SELECT * FROM plyinfo"))... 1: group = R uid = UNKNOWN 2: group = R uid = STEAM_0:1:15458550 3: group = R uid = STEAM_ID_PENDING ] lua_run PrintTable(sql.Query("SELECT group FROM plyinfo")) > PrintTable(sql.Query("SELECT group FROM plyinfo"))... includes\util.lua:35: bad argument #1 to 'pairs' (table expected, got boolean) ] lua_run PrintTable(sql.Query("SELECT group(*) FROM plyinfo")) > PrintTable(sql.Query("SELECT group(*) FROM plyinfo"))... includes\util.lua:35: bad argument #1 to 'pairs' (table expected, got boolean) [/code]
Print letter. Tell me what it does.
You know what? i forgot about k. Thanks guys!! You can remove/close this now!
Sorry, you need to Log In to post a reply to this thread.