tMySQL incorrect SQL syntax

(Uses tMySQL as said in the title.)
Hey everyone, I’m working with SQL for my first time and I have an issue where I’m trying to insert into the tables if it’s the players first time joining but to be honest I’m not sure what I’m doing.

Here’s what I have:
[lua]
hook.Add(“Initialize”,“Create Main Table”,function()
tmysql.query("CREATE TABLE IF NOT EXISTS db_player (unid varchar(255), player varchar(255), inv varchar(255)) ",function(res,status,err)
if err != 0 then
print(“ERR: “…err)
print(”[DB] Creating a new DB table, first time launching server?”)
end
if res then
print(res)
end
print(status)
end)
end)

function pl:Register()
self.Inventory = {}
tmysql.query(“INSERT INTO db_player (unid '”…tostring(self:SteamID())…"’,0,’"…glon.encode(self.Inventory)…"’) IF NOT SELECT unid FROM db_player WHERE unid = ‘"…tostring(self:SteamID())…"’",function(res,status,err)
end)
end
[/lua]



You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''STEAM_0:1,0,'') IF NOT SELECT unid FROM db_player WHERE unid = 'STE' at line 1


Any insight on what I should do? Like I said this is my first time working with SQL so I’m not sure what to do.

what the hell is if not select?

try:
[lua]
tmysql.query(“SELECT unid FROM db_player WHERE unid = '”…self:SteamID()…"’",
function(res, status, err)
if (res[1] && res[1][1])
// record exists!
else
// record doesnt exist
tmysql.query(“INSERT INTO db_player (unid '”…self:SteamID()…"’, 0 , ‘"…glon.encode(self.Inventory)…"’");
end
end
end)
[/lua]

[editline]24th July 2012[/editline]

also _R.Player:SteamID() returns a string so [lua]tostring(self:SteamID())[/lua] is redundant

Thanks I wasn’t sure of what I was doing. I wanted to make it safe for SQL but I guess SteamID was already a string :v and I wasn’t sure of how to return the data.

I appreciate it!

because the steamid doesnt contain any foreign characters such as quotes or \ its safe to use with sql queries :slight_smile: