Is this possible?

Hi guys!

Well, I’ve just learned how to add mySQL syntax into my gamemode, but all what I get are just errors.
Here is a part of the code what I’ve added:


RunConsoleCommand( “eng_ccsetname”, CharacterMenu.NameEntry:GetValue() );
RunConsoleCommand( “eng_ccsetage”, CharacterMenu.AgeEntry:GetValue() );
RunConsoleCommand( “eng_ccsetmodel”, ChosenCharacterModel );
RunConsoleCommand( “eng_ccsetstats”, ClientVars[“Strength”], ClientVars[“Endurance”], ClientVars[“Speed”], ClientVars[“Aim”] );
RunConsoleCommand( “eng_createcharacter”, “” );
ply:ChatPrint(“Character Stuff set!”);

	--Begin of Saving Character Info to mySQL--
	mysql.Query(db, "UPDATE player_info SET name = "..CharacterMenu.NameEntry:GetValue().." WHERE unique_id = '"..unique_id.."'");
	mysql.Query(db, "UPDATE player_info SET age = "..CharacterMenu.AgeEntry:GetValue().." WHERE unique_id = '"..unique_id.."'");
	mysql.Query(db, "UPDATE player_info SET model = "..ChosenCharacterModel.." WHERE unique_id = '"..unique_id.."'");
	mysql.Query(db, "UPDATE player_info SET strengh = "..ClientVars["Strength"]..", endurance = "..ClientVars["Endurance"]..", speed = "..ClientVars["Speed"]..", aim = "..ClientVars["Aim"].." WHERE unique_id = '"..unique_id.."'");
	ply:ChatPrint("Character has been saved!");
	--End of Saving Character Info to mySQL--

--for k,v in pairs blabla start--
local function ShowAllChar()
	for k, v in pairs( sql.Query("SELECT name, age, model, strengh, endurance, speed, aim, money FROM player_info WHERE unique_id = '"..SteamID.."'") ) do
		ply:ChatPrint("Character "" has "" Euros and is "..age.." old!");
		--Soon, I will change it to something else! This one is just for Test-Purpose xD
--for k,v in pairs blabla end--


…and sql_database.lua

local function mysql_value_char ( ply )
unique_id = mysql.Query(“SELECT unique_id FROM player_info WHERE unique_id = '”…SteamID…"’");
name = mysql.Query(db, “SELECT name FROM player_info WHERE unique_id = '”…SteamID…"’");
age = mysql.Query(db, “SELECT age FROM player_info WHERE unique_id = '”…SteamID…"’");
model = mysql.Query(db, “SELECT model FROM player_info WHERE unique_id = '”…SteamID…"’");
strengh = mysql.Query(db, “SELECT strengh FROM player_info WHERE unique_id = '”…SteamID…"’");
endurance = mysql.Query(db, “SELECT endurance FROM player_info WHERE unique_id = '”…SteamID…"’");
speed = mysql.Query(db, “SELECT speed FROM player_info WHERE unique_id = '”…SteamID…"’");
aim = mysql.Query(db, “SELECT aim FROM player_info WHERE unique_id = '”…SteamID…"’");
money = mysql.Query(db, “SELECT money FROM player_info WHERE unique_id = '”…SteamID…"’");
ply:SetNWString(“unique_id”, unique_id);
ply:SetNWString(“name”, name);
ply:SetNWInt(“age”, age);
ply:SetNWString(“model”, model);
ply:SetNWInt(“strengh”, strengh);
ply:SetNWInt(“endurance”, endurance);
ply:SetNWInt(“speed”, speed);
ply:SetNWInt(“aim”, aim);
ply:SetNWInt(“money”, money);

local function SaveStat ( ply )
money = ply:GetNWInt(“money”);
unique_id = ply:GetNWString(“unique_id”);
name = ply:GetNWString(“name”);
age = ply:GetNWInt(“age”);
model = ply:GetNWString(“model”);
strengh = ply:GetNWInt(“strengh”);
endurance = ply:GetNWInt(“endurance”);
speed = ply:GetNWInt(“speed”);
aim = ply:GetNWInt(“aim”);
mysql.Query(db, “UPDATE player_info SET money = “…money…”, name = “…name…”, age = “…age…”, model = “…model…”, strengh = “…strengh…”, endurance = “…endurance…”, speed = “…speed…”, aim = “…aim…” WHERE unique_id = '”…unique_id…"’");
ply:ChatPrint(“Your Character has been updated!”);

function player_exists( ply )

SteamID = ply:GetNWString("SteamID")

result = mysql.Query(db, "SELECT unique_id, money, name, age, model, strengh, endurance, speed, aim FROM player_info WHERE unique_id = '"..SteamID.."'");
if (result) then
	mysql_value_char( ply );
	ply:PrintChat("Player does not exist! Please create one though the Character Create window!");


function Initialize()

function PlayerInitialSpawn( ply )

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


hook.Add( “PlayerInitialSpawn”, “PlayerInitialSpawn”, PlayerInitialSpawn )
hook.Add( “Initialize”, “Initialize”, Initialize )

So, here’s the problem: I want that GMod saves my SteamID (and the player’s one) into the mySQL database and it should be able to call it through “unique_id” and “SteamID”.

But I don’t get my SteamID, but for that I get a nil value.

So, do you maybe know what’s the problem in my script?
I would really thank you, when you want to help me.

P. S.: Yes, it is the Rick’s leaked version of TacoScript2. I am now adding some functions and mySQL stuff into it aka I want to finish it.

I dunno about MySQL, I used the convenient SQLite built-in to Gmod.

Also, two other things:

Instead of directly putting the SteamID string in, you need format the string for sql use using the sql.SQLStr function.

Also, to retrieve a value use sql.QueryValue instead of just Query, along with tonumber or tostring to be safe; for example:

unique_id = tostring(sql.Query("SELECT unique_id FROM player_info WHERE unique_id = '"..sql.SQLStr(SteamID).."'"))

Well, maybe this will work… I’ll gunna give it a try.
Thanks for helping me out!


And can I use also SQLite? Hell yeah!