Player stats not saving everything
#1

For some reason my MYSQL is not saving player stats properly. Instead of saving what I'm actually trying to output, it only saves "0"'s.





PHP код:
stock SavePlayerStats(playerid)
{
    new 
query[1000], query2[1000];
    
format(querysizeof(query), "UPDATE `accounts` SET `playerCash` = '%d', `playerAdmin` = '%d', `playerMod` = '%d', `playerHelper` = '%d', `playerLevel` = '%d' WHERE `playerName` = '%s'"GetPlayerMoney(playerid), playerInfo[playerid][playerAdmin],playerInfo[playerid][playerMod],playerInfo[playerid][playerHelper],GetPlayerScore(playerid), playerInfo[playerid][playerName]);
       
mysql_tquery(Databasequery);
    
format(querysizeof(query2), "UPDATE `accounts` SET `playerAge` = '%d', `playerGend` = '%d', `playerSkin` = '%d'"playerInfo[playerid][playerAge],playerInfo[playerid][playerGend], playerInfo[playerid][playerSkin], playerInfo[playerid][playerName]);
       
mysql_tquery(Databasequery2);
       
printf("Player saved.");

Reply
#2

Can you put a print statement between the lines and post those as well, so we can see what the values are that are being inserted into the query?
You can do that to debug your query and see what truely gets sent to your mysql server.

Also a note of concern:
You should use mysql_format and use %e instead of %s for strings to prevent mysql injection.
Mysql injection could be used to wipe your entire database just by using a specific playername.

Also don't use mysql as being a filesystem where you have to rewrite the entire file when 1 value changes.

You have to ask yourself:
How often does the player's age, gender and skin change?
And how often does his admin status change?
You don't need a query to update those values everytime a player's money changes.

If the money changes, change only the money, nothing else.
If the gender would change, update it when it changes. If it doesn't change, leave it alone.
Your letting mysql work harder for nothing.

Mysql needs to examine your query, break it down into several sections, find the correct table, column and rows and check if the given value is the same type (an integer) as the value stored in that field.
To add to that, it first checks if the value stored isn't the same as the value given, because overwriting a value means mysql needs to search through the file and overwrite that specific byte or collection of bytes and means disk-access, which is slow.
If the value doesn't change, mysql won't do a thing and your entire query does nothing but waste processor power and memory.

And it does this for EVERY value given.
So trying to update 7 values instead of just 1 means you're wasting the processing power for 6 queries that do nothing at all.

If you write a letter and you make a typo and you need to change 1 character in your text, do you wipe it all out and write it from scratch, updating every character or would you just wipe that one character and overwrite that?
I guess you would do the latter.

Don't waste processing power to do useless work.

Use smaller functions to do one thing like this one:
PHP код:
// This function is used to give (or take) money to/from the player
Player_GiveMoney(playeridamount)
{
    
// Setup local variables
    
new query[128];
    
// Add the given money to the player's account
    
APlayerData[playerid][Money] = APlayerData[playerid][Money] + amount;
    
// Also update the client immediately instead of waiting for the timer to update it after one second
    
ResetPlayerMoney(playerid);
    
GivePlayerMoney(playeridAPlayerData[playerid][Money]);
    
// Update money for this player in the player's account in MySQL
    
mysql_format(SQL_dbquerysizeof(query), "UPDATE playerdata SET Money = '%i' WHERE UserID = '%i'"APlayerData[playerid][Money], APlayerData[playerid][UserID]);
    
mysql_tquery(SQL_dbquery"""");
    return 
1;

This function gives the player his money ingame and creates a query to update it in the database.
You could use many small functions like this one to also change the scorepoints, gender, skin, whatever.
Reply
#3

Thanks for a quick reply

It saves onplayerdisconnect, and I put the print in under both statements. This is what happened in the commandline:
PHP код:
[12:21:59UPDATE `accountsSET `playerCash` = '0', `playerAdmin` = '0', `playerMod` = '0', `playerHelper` = '0', `playerLevel` = '1' WHERE `playerName` = 'yllo'
[12:21:59] (null
The second mysql_format seems to not do anything and returns null. All the player details apart from level are not set as of yet.
Reply
#4

Hi again, got another problem.

I've got it so it now saves the player details but then loads all the previous data from the previous player.
I'm unsure why aswell... itis set to look at the rows ect ect

PHP код:
forward LoadPlayerStats(playerid);
public 
LoadPlayerStats(playerid)
{
    
//if(playerInfo[playerid][LoggedIn] ==  true)
     //{
        
new query[200], name[24];
        
GetPlayerName(playeridname24);
        
format(querysizeof(query), "SELECT * FROM `accounts` WHERE `playerName` = '%s' LIMIT 1"playerInfo[playerid][playerName]);
          
mysql_tquery(Databasequery);
          if(
cache_num_rows())
          {
              
SetPlayerVirtualWorld(playerid0);
              
cache_get_value_int(0"playerAdmin"playerInfo[playerid][playerAdmin]);
            
cache_get_value_int(0"playerMod"playerInfo[playerid][playerMod]);
            
cache_get_value_int(0"playerHelper"playerInfo[playerid][playerHelper]);
            
cache_get_value_int(0"playerLevel"playerInfo[playerid][playerLevel]);
            
cache_get_value_int(0"playerCash"playerInfo[playerid][playerCash]);
            
cache_get_value_int(0"playerAge"playerInfo[playerid][playerAge]);
            
cache_get_value_int(0"playerGend"playerInfo[playerid][playerGend]);
            
cache_get_value_int(0"playerSkin"playerInfo[playerid][playerSkin]);
            
cache_get_value_int(0"playerFact"playerInfo[playerid][playerFact]);
            
cache_get_value_int(0"playerFactR"playerInfo[playerid][playerFactR]);
            
cache_get_value(0"Rankname"playerInfo[playerid][Rankname],32);
            
cache_get_value_name_float(0"playerX"playerInfo[playerid][playerX]);
            
cache_get_value_name_float(0"playerY"playerInfo[playerid][playerY]);
            
cache_get_value_name_float(0"playerZ"playerInfo[playerid][playerZ]);
        print(
query);
        }
        
SetSpawnInfo(playerid0,playerInfo[playerid][playerSkin],playerInfo[playerid][playerX],playerInfo[playerid][playerY], playerInfo[playerid][playerZ], 00000,0,0);
        
SetPlayerPos(playerid,playerInfo[playerid][playerX],playerInfo[playerid][playerY], playerInfo[playerid][playerZ]);
        
SetPlayerScore(playeridplayerInfo[playerid][playerLevel]);
          
GivePlayerMoney(playeridplayerInfo[playerid][playerCash]);
        
//cache_delete(result);
        
printf("player loaded.");
        return 
1;
    
//}

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)