PDA

View Full Version : MySQL doesn't save


x96664
26/09/2013, 08:38 AM
Hi guys, I have a problem with my MySQL system, it doesn't save player stats.
public OnPlayerDisconnect(playerid, reason)
{
if(Logged[playerid] == 1)
{
new query[128];
format (query, sizeof(query), "SELECT `Name` FROM `players` WHERE `Name` = '%s' LIMIT 0,1", GetPName(playerid));
mysql_function_query(IMDb, query, true, "SaveStats", "i", playerid);
}
return 1;
}

public SaveStats(playerid)
{
new rows, fields;
cache_get_data(rows, fields);
if(rows)
{
new query[512], Float:sHealth, Float:sArmor, Float:sX, Float:sY, Float:sZ, Float:sA;

GetPlayerHealth(playerid, sHealth);
GetPlayerArmour(playerid, sArmor);
GetPlayerPos(playerid, sX, sY, sZ);
GetPlayerFacingAngle(playerid, sA);

format(query, sizeof(query), "UPDATE players SET Health = %.1f, Armor = %.1f, Interior = %i, VW = %i, Skin = %i, X = %.1f, Y = %.1f, Z = %.1f, A = %.1f WHERE Name = '%s'", sHealth, sArmor, GetPlayerInterior(playerid), GetPlayerVirtualWorld(playerid), GetPlayerSkin(playerid), sX, sY, sZ, sA, gName);
mysql_function_query(IMDb, query, false, "", "");
}
return 1;
}


When I leave the server it doesn't save none of these.

Konstantinos
26/09/2013, 10:06 AM
Erm, why don't you update the data in OnPlayerDisconnect instead of selecting first, check if there are rows.. until then, the player won't be in the server anymore.

Moreover, does it print anything in mysql log?

zT KiNgKoNg
26/09/2013, 10:10 AM
Okay here's an example that you could take and use for the script you're using or creating. (Taken from my custom script)


stock SaveStats(playerid)
{


format(Query,sizeof(Query),"UPDATE `Accounts` SET `Time` = '%s' WHERE `Username` = '%s'",
GetTime(),
GetOriginalName(playerid));

mysql_query(Query);
mysql_free_result();
return 1;
}

Konstantinos
26/09/2013, 10:12 AM
Okay here's an example that you could take and use for the script you're using or creating. (Taken from my custom script)


stock SaveStats(playerid)
{


format(Query,sizeof(Query),"UPDATE `Accounts` SET `Time` = '%s' WHERE `Username` = '%s'",
GetTime(),
GetOriginalName(playerid));

mysql_query(Query);
mysql_free_result();
return 1;
}


He uses threaded queries - something you don't (and you should).

zT KiNgKoNg
26/09/2013, 10:24 AM
He uses threaded queries - something you don't (and you should).

As this section of code is from a old version of the script, and i will not be giving out anything on the updated version.

x96664
26/09/2013, 10:36 AM
Erm, why don't you update the data in OnPlayerDisconnect instead of selecting first, check if there are rows.. until then, the player won't be in the server anymore.

Moreover, does it print anything in mysql log?

Well because if the player isn't logged/regged for some reason it shouldnt save him.
No I had some warnings because of the size but I fixed them and now the logs doesnt print anything.

Konstantinos
26/09/2013, 10:41 AM
Then check if they are!

public OnPlayerDisconnect(playerid, reason)
{
if(Logged[playerid] == 1)
{
new query[512], Float:sHealth, Float:sArmor, Float:sX, Float:sY, Float:sZ, Float:sA;

GetPlayerHealth(playerid, sHealth);
GetPlayerArmour(playerid, sArmor);
GetPlayerPos(playerid, sX, sY, sZ);
GetPlayerFacingAngle(playerid, sA);

format(query, sizeof(query), "UPDATE players SET Health = %.1f, Armor = %.1f, Interior = %i, VW = %i, Skin = %i, X = %.1f, Y = %.1f, Z = %.1f, A = %.1f WHERE Name = '%s'", sHealth, sArmor, GetPlayerInterior(playerid), GetPlayerVirtualWorld(playerid), GetPlayerSkin(playerid), sX, sY, sZ, sA, gName);
mysql_function_query(IMDb, query, false, "", "");
}
return 1;
}


By the way, if it doesn't save either then you need to make sure for 2 things. That the player is actually logged in (Logged[playerid] is 1) and that the fields name are correct written or exist. You can make sure about that by running the query to phpMyAdmin.

x96664
26/09/2013, 11:10 AM
You can make sure about that by running the query to phpMyAdmin.

How actually to do it ?

Konstantinos
26/09/2013, 11:22 AM
How actually to do it ?


format(query, sizeof(query), "UPDATE players SET Health = %.1f, Armor = %.1f, Interior = %i, VW = %i, Skin = %i, X = %.1f, Y = %.1f, Z = %.1f, A = %.1f WHERE Name = '%s'", sHealth, sArmor, GetPlayerInterior(playerid), GetPlayerVirtualWorld(playerid), GetPlayerSkin(playerid), sX, sY, sZ, sA, gName);
print(query);


Copy the output, add ` ` around the fields' name and execute it to phpMyAdmin > select database, select table > SQL > paste > Go!

x96664
26/09/2013, 12:57 PM
Then check if they are!

public OnPlayerDisconnect(playerid, reason)
{
if(Logged[playerid] == 1)
{
new query[512], Float:sHealth, Float:sArmor, Float:sX, Float:sY, Float:sZ, Float:sA;

GetPlayerHealth(playerid, sHealth);
GetPlayerArmour(playerid, sArmor);
GetPlayerPos(playerid, sX, sY, sZ);
GetPlayerFacingAngle(playerid, sA);

format(query, sizeof(query), "UPDATE players SET Health = %.1f, Armor = %.1f, Interior = %i, VW = %i, Skin = %i, X = %.1f, Y = %.1f, Z = %.1f, A = %.1f WHERE Name = '%s'", sHealth, sArmor, GetPlayerInterior(playerid), GetPlayerVirtualWorld(playerid), GetPlayerSkin(playerid), sX, sY, sZ, sA, gName);
mysql_function_query(IMDb, query, false, "", "");
}
return 1;
}


By the way, if it doesn't save either then you need to make sure for 2 things. That the player is actually logged in (Logged[playerid] is 1) and that the fields name are correct written or exist. You can make sure about that by running the query to phpMyAdmin.

Actually this works, and there is the print:
UPDATE players SET `Health` = 100.0, `Armor` = 0.0, `Interior` = 0, `VW` = 0, `Skin` = 0, `X` = 1958.3, `Y` = 1343.1, `Z` = 15.3, `A` = 269.1 WHERE `Name` = 'Ryder_Ballaz'
So it now saves, thank you! REP+