SQL problem

We’ll i copied the tut off of gmod.com but and changed it a little but everytime i start up my gamemode that has it it keeps putting out this error: Timer Error: RPG/gamemode/init.lua:69: attempt to concatenate global ‘XP’ (a boolean value)

here’s my code[lua]
function GM:PlayerInitialSpawn( ply )

 ply:ConCommand( "classmenu" ) 
	timer.Create("Steam_id_delay", 1, 1, function()
	SteamID = ply:SteamID()
	ply:SetNWString("SteamID", SteamID)
	timer.Create("SaveStat", 10, 0, function() saveStat( ply ) end)
	player_exists( ply ) 
end)

end

function sql_value_stats ( ply )
unique_id = sql.QueryValue(“SELECT unique_id FROM player_info WHERE unique_id = '”…steamID…"’")
money = sql.QueryValue(“SELECT money FROM player_info WHERE unique_id = '”…steamID…"’")
XP = sql.QueryValue(“SELECT XP FROM player_info WHERE unique_id = '”…steamID…"’")
ply:SetNWString(“unique_id”, unique_id)
ply:SetNWInt(“money”, money)
ply:SetNWInt(“XP”,XP)
end

function sql_value_skills ( ply )
unique_id = sql.QueryValue(“SELECT unique_id FROM player_skills WHERE unique_id = '”…steamID…"’")

Melee = sql.QueryValue("SELECT Melee FROM player_skills WHERE unique_id = '"..steamID.."'")
Magic = sql.QueryValue("SELECT Magic FROM player_skills WHERE unique_id = '"..steamID.."'")
ply:SetNWString("unique_id", unique_id)

ply:SetNWInt("Melee", Melee)
ply:SetNWInt("Magic", Magic)

end

function saveStat ( ply )
money = ply:GetNWInt(“money”)
XP = ply:GetNWInt(“XP”)
unique_id = ply:GetNWString (“SteamID”)

Melee = ply:GetNWInt("Melee")
	Magic = ply:GetNWInt("Magic")
sql.Query("UPDATE player_skills SET Melee = "..Melee..", Magic = "..Magic.." WHERE unique_id = '"..unique_id.."'")
sql.Query("UPDATE player_info SET money = "..money..",XP"..XP.." WHERE unique_id = '"..unique_id.."'")
ply:ChatPrint("Stats updated !")

end

function tables_exist()

if (sql.TableExists("player_info") && sql.TableExists("player_skills")) then
	Msg("Both tables already exist !")
else
	if (!sql.TableExists("player_info")) then
		query = "CREATE TABLE player_info ( unique_id varchar(255), money int ,XP int)"
		result = sql.Query(query)
		if (sql.TableExists("player_info")) then
			Msg("Succes ! table 1 created 

")
else
Msg("Somthing went wrong with the player_info query !
")
Msg( sql.LastError( result ) … "
" )
end
end
if (!sql.TableExists(“player_skills”)) then
query = “CREATE TABLE player_skills ( unique_id varchar(255), Melee int, Magic int )”
result = sql.Query(query)
if (sql.TableExists(“player_skills”)) then
Msg("Succes ! table 2 created
")
else
Msg("Somthing went wrong with the player_skills query !
")
Msg( sql.LastError( result ) … "
" )
end
end
end

end

function new_player( SteamID, ply )

	steamID = SteamID
	sql.Query( "INSERT INTO player_info (`unique_id`, `money`,`XP`)VALUES ('"..steamID.."', '100')" )
	result = sql.Query( "SELECT unique_id, money ,XP FROM player_info WHERE unique_id = '"..steamID.."'" )
	if (result) then

		sql.Query( "INSERT INTO player_skills (`unique_id`, `Melee`, `Magic`)VALUES ('"..steamID.."', '1', '1', '1')" )
		result = sql.Query( "SELECT unique_id,Melee, Magic FROM player_skills WHERE unique_id = '"..steamID.."'" )
		if (result) then
			Msg("Player account created !

")
sql_value_stats( ply )
sql_value_skills( ply )
else
Msg("Something went wrong with creating a players skills !
")
end

	else
		Msg("Something went wrong with creating a players info !

")
end
end

function player_exists( ply )

steamID = ply:GetNWString("SteamID")

result = sql.Query("SELECT unique_id, money FROM player_info WHERE unique_id = '"..steamID.."'")
if (result) then
		sql_value_stats( ply ) // We will call this to retrieve the stats
		sql_value_skills( ply ) // We will call this to retrieve the skills
else
	new_player( steamID, ply ) // Create a new player :D
end

end

function Initialize()
tables_exist()
end
[/lua]

Can you link the tutorial that you used? And also can you post the code you have with all the white space included?
Concatenate is just a fancy term for adding two strings together, in case you aren’t aware. The problem might lie in you trying to sending the query a datatype that it doesn’t like.

Well seeing as there is only one SQL tutorial for lua ( Part 1,Part 2, Part 3 ) He used that one, and It does look fine, because It’s basically how I used it, except i did not include the player skills table because im not doing player skills for my gamemode.

I used that tut posted above and thats all my code that is related to sql i haven’t been able to implement it yet into my gamemode.

Well any code you make that has anyhing to do with SQL would go in init.lua ( Serverside )

It is.

Have you got them in the right order?

They go:


function sql_value_stats ( ply )
function sql_value_skills ( ply )
function saveStat ( ply )
function tables_exist()
function new_player( SteamID, ply )
function player_exists( ply )
function Initialize()
function PlayerInitialSpawn( ply )
hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawn", PlayerInitialSpawn )
hook.Add( "Initialize", "Initialize", Initialize )

All those in that order, or it will not work.

Oh I see what it is, you’ve got the GM:PlayerInitialSpawn calling things that have not been defined yet in the code, you must put it after all of the code.

[editline]08:45PM[/editline]

It should be:
[lua]function sql_value_stats ( ply )
unique_id = sql.QueryValue(“SELECT unique_id FROM player_info WHERE unique_id = '”…steamID…"’")
money = sql.QueryValue(“SELECT money FROM player_info WHERE unique_id = '”…steamID…"’")
XP = sql.QueryValue(“SELECT XP FROM player_info WHERE unique_id = '”…steamID…"’")
ply:SetNWString(“unique_id”, unique_id)
ply:SetNWInt(“money”, money)
ply:SetNWInt(“XP”,XP)
end

function sql_value_skills ( ply )
unique_id = sql.QueryValue(“SELECT unique_id FROM player_skills WHERE unique_id = '”…steamID…"’")

Melee = sql.QueryValue("SELECT Melee FROM player_skills WHERE unique_id = '"..steamID.."'")
Magic = sql.QueryValue("SELECT Magic FROM player_skills WHERE unique_id = '"..steamID.."'")
ply:SetNWString("unique_id", unique_id)

ply:SetNWInt("Melee", Melee)
ply:SetNWInt("Magic", Magic)

end

function saveStat ( ply )
money = ply:GetNWInt(“money”)
XP = ply:GetNWInt(“XP”)
unique_id = ply:GetNWString (“SteamID”)

Melee = ply:GetNWInt("Melee")
    Magic = ply:GetNWInt("Magic")
sql.Query("UPDATE player_skills SET Melee = "..Melee..", Magic = "..Magic.." WHERE unique_id = '"..unique_id.."'")
sql.Query("UPDATE player_info SET money = "..money..",XP"..XP.." WHERE unique_id = '"..unique_id.."'")
ply:ChatPrint("Stats updated !")

end

function tables_exist()

if (sql.TableExists("player_info") && sql.TableExists("player_skills")) then
    Msg("Both tables already exist !")
else
    if (!sql.TableExists("player_info")) then
        query = "CREATE TABLE player_info ( unique_id varchar(255), money int ,XP int)"
        result = sql.Query(query)
        if (sql.TableExists("player_info")) then
            Msg("Succes ! table 1 created 

")
else
Msg("Somthing went wrong with the player_info query !
")
Msg( sql.LastError( result ) … "
" )
end
end
if (!sql.TableExists(“player_skills”)) then
query = “CREATE TABLE player_skills ( unique_id varchar(255), Melee int, Magic int )”
result = sql.Query(query)
if (sql.TableExists(“player_skills”)) then
Msg("Succes ! table 2 created
")
else
Msg("Somthing went wrong with the player_skills query !
")
Msg( sql.LastError( result ) … "
" )
end
end
end

end

function new_player( SteamID, ply )

    steamID = SteamID
    sql.Query( "INSERT INTO player_info (`unique_id`, `money`,`XP`)VALUES ('"..steamID.."', '100')" )
    result = sql.Query( "SELECT unique_id, money ,XP FROM player_info WHERE unique_id = '"..steamID.."'" )
    if (result) then

        sql.Query( "INSERT INTO player_skills (`unique_id`, `Melee`, `Magic`)VALUES ('"..steamID.."', '1', '1', '1')" )
        result = sql.Query( "SELECT unique_id,Melee, Magic FROM player_skills WHERE unique_id = '"..steamID.."'" )
        if (result) then
            Msg("Player account created !

")
sql_value_stats( ply )
sql_value_skills( ply )
else
Msg("Something went wrong with creating a players skills !
")
end

    else
        Msg("Something went wrong with creating a players info !

")
end
end

function player_exists( ply )

steamID = ply:GetNWString("SteamID")

result = sql.Query("SELECT unique_id, money FROM player_info WHERE unique_id = '"..steamID.."'")
if (result) then
        sql_value_stats( ply ) // We will call this to retrieve the stats
        sql_value_skills( ply ) // We will call this to retrieve the skills
else
    new_player( steamID, ply ) // Create a new player :D
end

end

function Initialize()
tables_exist()
end

function GM:PlayerInitialSpawn( ply )

 ply:ConCommand( "classmenu" ) 
    timer.Create("Steam_id_delay", 1, 1, function()
    SteamID = ply:SteamID()
    ply:SetNWString("SteamID", SteamID)
    timer.Create("SaveStat", 10, 0, function() saveStat( ply ) end)
    player_exists( ply ) 
end)

end
hook.Add( “PlayerInitialSpawn”, “PlayerInitialSpawn”, GM:PlayerInitialSpawn )
hook.Add( “Initialize”, “Initialize”, Initialize )
[/lua]

That doesn’t make sense, thats not how Lua works…

There is something else wrong with your code.

First of all:
Your missing an = right here, next to XP:
[lua] sql.Query(“UPDATE player_info SET money = “…money…”,XP”…XP…" WHERE unique_id = ‘"…unique_id…"’") [/lua]

It does, because in his code, He calls on the NWStrings before he’s set any, and then at the very end of his code, he sets the NWStrings, they have to be set before you call them.

If he calls them before they are set then yes. But the ORDER in which the code is written isn’t relevant.

And that is what I thought you did: Just change the order of the function :confused: