MySQL saving/loading.
#1

Hey,

I am trying to learn MySQL.
But im stuck at the saving and loading part. Somehow, the stats don't save/load.
Cause I'm new with SQL, im sure i made a very basic mistake. Can anyone look to this code's and help me?

Код:
public SavePlayerStats(playerid)
{
    new Float:hp;
    new Float:am;
    new Float:saveX, Float:saveY, Float:saveZ;
    GetPlayerName(playerid, pname, sizeof (pname));
    GetPlayerIp(playerid, ip, 16);
	GetPlayerHealth(playerid,hp);
	GetPlayerArmour(playerid,am);
	GetPlayerPos(playerid, saveX, saveY, saveZ);
	pInfo[playerid][money] = GetPlayerMoneyEx(playerid);
	SaveWeaponsToFile(playerid);
	format(file, sizeof file,"UPDATE `users` SET `money` = '%d', `kills` = '%d', `deaths` = '%d', `adminlevel` = '%d', `license` = '%d', `job` = '%d', WHERE `username` = '%s'",pInfo[playerid][money],pInfo[playerid][kills],pInfo[playerid][deaths],pInfo[playerid][adminlevel],pInfo[playerid][license],pInfo[playerid][job],pname);
	mysql_query(file);
	format(file, sizeof file,"UPDATE `users` SET `weaponlicense` = '%d', `flyinglicense` = '%d', `bankmoney` = '%d', `skin` = '%d', `fightstyle` = '%d', `beer` = '%d', `cigarets` = '%d', `chainlock` = '%d', `sprunk` = '%d', WHERE `username` = '%s'",pInfo[playerid][weaponlic],pInfo[playerid][flylic],pInfo[playerid][bankmoney],pInfo[playerid][skin],pInfo[playerid][fightstyle],pInfo[playerid][beer],pInfo[playerid][cigarets],pInfo[playerid][chainlock],pInfo[playerid][sprunk],pname);
	mysql_query(file);
	format(file, sizeof file,"UPDATE `users` SET `health` = '%f' WHERE `username` = '%s'",floatround(hp),pname);
	if(hp != 0.0) mysql_query(file);
	format(file, sizeof file,"UPDATE `users` SET `armour` = '%f' WHERE `username` = '%s'",floatround(am),pname);
	if(am != 0.0) mysql_query(file);
	format(file, sizeof file,"UPDATE `users` SET `spawnX` = '%0.3f' WHERE `username` = '%s'",saveX,pname);
	if(saveX != 0.0) mysql_query(file);
	format(file, sizeof file,"UPDATE `users` SET `spawnY` = '%0.3f' WHERE `username` = '%s'",saveY,pname);
	if(saveY != 0.0) mysql_query(file);
	format(file, sizeof file,"UPDATE `users` SET `spawnZ` = '%0.3f' WHERE `username` = '%s'",saveZ,pname);
	if(saveZ != 0.0) mysql_query(file);
	//-
	format(file, sizeof file,"UPDATE `users` SET `cellphone` = '%d' WHERE `username` = '%s'",pInfo[playerid][havephone],pname);
	mysql_query(file);
	format(file, sizeof file,"UPDATE `users` SET `weed` = '%d' WHERE `username` = '%s'",pInfo[playerid][weed],pname);
	mysql_query(file);
	format(file, sizeof file,"UPDATE `users` SET `seeds` = '%d' WHERE `username` = '%s'",pInfo[playerid][seeds],pname);
	mysql_query(file);
	format(file, sizeof file,"UPDATE `users` SET `prepaid` = '%d' WHERE `username` = '%s'",pInfo[playerid][prepaid],pname);
	mysql_query(file);
	format(file, sizeof file,"UPDATE `users` SET `jerrycan` = '%d' WHERE `username` = '%s'",pInfo[playerid][jerrycan],pname);
	mysql_query(file);
	format(file, sizeof file,"UPDATE `users` SET `dlock` = '%d' WHERE `username` = '%s'",pInfo[playerid][dlock],pname);
	mysql_query(file);
	format(file, sizeof file,"UPDATE `users` SET `gps` = '%d' WHERE `username` = '%s'",pInfo[playerid][GPS],pname);
	mysql_query(file);
	format(file, sizeof file,"UPDATE `users` SET `warns` = '%d' WHERE `username` = '%s'",pInfo[playerid][warns],pname);
	mysql_query(file);
	format(file, sizeof file,"UPDATE `users` SET `vip` = '%d' WHERE `username` = '%s'",pInfo[playerid][VIP],pname);
	mysql_query(file);
	format(file, sizeof file,"UPDATE `users` SET `ip adress` = '%s' WHERE `username` = '%s'",ip,pname);
	mysql_query(file);
    SavePlayerFactionInfo(playerid);
	//print("SavePlayerStats Called!");
}

public LoadPlayerStats(playerid)
{
    new Float:POS[3];
    GetPlayerName(playerid, pname, sizeof (pname));
    format(file, sizeof file,"SELECT `money` FROM `users` WHERE `username` = '%s'",pname);
    pInfo[playerid][money] = mysql_query(file);
    format(file, sizeof file,"SELECT `license` FROM `users` WHERE `username` = '%s'",pname);
    pInfo[playerid][license]  = mysql_query(file);
    format(file, sizeof file,"SELECT `weaponlicense` FROM `users` WHERE `username` = '%s'", pname);
    pInfo[playerid][weaponlic]  = mysql_query(file);
    format(file, sizeof file,"SELECT `flylicense` FROM `users` WHERE `username` = '%s'", pname);
    pInfo[playerid][flylic]  = mysql_query(file);
    format(file, sizeof file,"SELECT `fightstyle` FROM `users` WHERE `username` = '%s'", pname);
    pInfo[playerid][fightstyle] = mysql_query(file);
    format(file, sizeof file,"SELECT `adminlevel` FROM `users` WHERE `username` = '%s'", pname);
    pInfo[playerid][adminlevel] = mysql_query(file);
    format(file, sizeof file,"SELECT `jerrycan` FROM `users` WHERE `username` = '%s'", pname);
	pInfo[playerid][jerrycan] = mysql_query(file);
	format(file, sizeof file,"SELECT `weed` FROM `users` WHERE `username` = '%s'", pname);
   	pInfo[playerid][weed] = mysql_query(file);
   	format(file, sizeof file,"SELECT `seeds` FROM `users` WHERE `username` = '%s'", pname);
	pInfo[playerid][seeds] = mysql_query(file);
	format(file, sizeof file,"SELECT `beer` FROM `users` WHERE `username` = '%s'", pname);
	pInfo[playerid][beer] = mysql_query(file);
	format(file, sizeof file,"SELECT `sprunk` FROM `users` WHERE `username` = '%s'", pname);
	pInfo[playerid][sprunk] = mysql_query(file);
	format(file, sizeof file,"SELECT `cigarets` FROM `users` WHERE `username` = '%s'", pname);
	pInfo[playerid][cigarets] = mysql_query(file);
	format(file, sizeof file,"SELECT `chainlock` FROM `users` WHERE `username` = '%s'", pname);
	pInfo[playerid][chainlock] = mysql_query(file);
	format(file, sizeof file,"SELECT `spawnX` FROM `users` WHERE `username` = '%s'", pname);
	POS[0] = mysql_query(file);
	format(file, sizeof file, "SELECT `spawnY` FROM `users` WHERE `username` = '%s'", pname);
 	POS[1] = mysql_query(file);
 	format(file, sizeof file,"SELECT `spawnZ` FROM `users` WHERE `username` = '%s'", pname);
 	POS[2] = mysql_query(file);
 	format(file, sizeof file,"SELECT `cellphone` FROM `users` WHERE `username` = '%s'", pname);
 	pInfo[playerid][havephone] = mysql_query(file);
 	SetPlayerPos(playerid, POS[0], POS[1], POS[2]);
    LoadJob(playerid);
    LoadPlayerFactionInfo(playerid);
    GivePlayerMoneyEx(playerid, pInfo[playerid][money]);
    SetPlayerFightingStyle(playerid, pInfo[playerid][fightstyle]);
    LoadWeaponsToFile(playerid);
    SetTimerEx("Loadmorestats",100,false,"i",playerid);
    return 1;
}
Reply
#2

Okay, I don`t have much time and I hope you`re a fast learner. I`ll show you my Loading and Saving function for MySQL and a brief explanation and I hope you`ll understand.

Loading:
Код:
public LoadVariables(playerid)
{
	//connection
	mysql_debug(1);
	mysql_connect(HOST, USER, DATABASE, PASSWORD);
	//query
	new EscapeName[MAX_PLAYER_NAME], Name[MAX_PLAYER_NAME];
	GetPlayerName(playerid, Name, sizeof(Name));
	mysql_real_escape_string(Name, EscapeName);
	
	new Query[256]; format(Query, sizeof(Query), "SELECT * FROM users WHERE username = '%s'", EscapeName);
	mysql_query(Query);
	mysql_store_result();
	new Result[128]; mysql_fetch_row_format(Result, " ");
	//variables selection
	new username[50], p_password[30], level, alevel, money, xp, Float:lastX, Float:lastY, Float:lastZ, Float:lastRot, loginSpawn;
	sscanf(Result, "s[50]s[30]iiiiffffi", username, p_password, level, alevel, money, xp, lastX, lastY, lastZ, lastRot, loginSpawn);
	SetPVarString(playerid, "username", username);
	SetPVarString(playerid, "password", p_password);
	SetPVarInt(playerid, "level", level);
	SetPVarInt(playerid, "alevel", alevel);
	SetPVarInt(playerid, "money", money);
	SetPVarInt(playerid, "xp", xp);
	SetPVarFloat(playerid, "lastX", lastX);
	SetPVarFloat(playerid, "lastY", lastY);
	SetPVarFloat(playerid, "lastZ", lastZ);
	SetPVarFloat(playerid, "lastRot", lastRot);
	SetPVarInt(playerid, "loginSpawn", loginSpawn);
	SetPVarInt(playerid, "IsSpawnedFromIntro", 1);
	SetPVarInt(playerid, "DiscFromGMX", 0);
	//connection close
	mysql_close();
	return 1;
}
So, the idea is: You connect to the server, you format the query string, you then send the query, store the result, fetch the result into an array(!) and then use the sscanf to get your data from the string.

Saving:
Код:
:
public SaveVariables(playerid)
{
	//connection
	mysql_debug(1);
    mysql_connect(HOST, USER, DATABASE, PASSWORD);
	//query
	new EscapeName[MAX_PLAYER_NAME], Name[MAX_PLAYER_NAME];
	GetPlayerName(playerid, Name, sizeof(Name));
	mysql_real_escape_string(Name, EscapeName);
	
	new Float:lastX, Float:lastY, Float:lastZ, Float:lastRot;
	GetPlayerPos(playerid, lastX, lastY, lastZ);
	GetPlayerFacingAngle(playerid, lastRot);
	new Query[256]; format(Query, sizeof(Query), "UPDATE users SET level = '%i', alevel = '%i', money = '%i', xp = '%i', lastX = '%f', lastY = '%f', lastZ = '%f', lastRot = '%f' WHERE username = '%s'", GetPVarInt(playerid, "level"), GetPVarInt(playerid, "alevel"), GetPVarInt(playerid, "money"), GetPVarInt(playerid, "xp"), lastX, lastY, lastZ, lastRot, EscapeName);
	mysql_query(Query);
	//connection close
	mysql_close();
	return 1;
}
Here the idea is: You connect to the server, format the query string and then query the string. No further need to store any results since you don`t 'download' anything, you just 'upload'.

As a reminder in the end, when you interact with user input/name/etc always use mysql_real_escape_string(string, escape_string);

Hope you understood. If not, reply! Good luck!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)