Sql query not returning a table?

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)…"
")
for id, val in pairs(result) do
res = tostring(val[‘grp’])
end
Msg("Query is of value:
“)
Msg(res…”
")
if(result) then
Msg("Player: “…ply:Nick()…” belongs in team: “…res…”
")
return res
else
Msg("Player is not in the table. Query:
“)
Msg(query…”
")
return false
end
end
[/lua]
oh right, heres the error




ERROR: GAMEMODE:'PlayerSpawn' Failed: Testing/gamemode/init.lua:105: bad argument #1 to 'pairs' (table expected, got boolean)



Try using sql.QueryValue 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:




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'


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




ERROR: GAMEMODE:'PlayerSpawn' Failed: Testing/gamemode/init.lua:122: attempt to concatenate global 'Team' (a nil value)



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:



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)


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?




] 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)


Print letter. Tell me what it does.

You know what? i forgot about k. Thanks guys!! You can remove/close this now!