SA-MP Forums Archive
MySQL help - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: MySQL help (/showthread.php?tid=346122)



MySQL help - Elysian` - 27.05.2012

How would I save a players score? I'm not sure on where to add it?

pawn Код:
mysql_fetch_field_row(szReturn, "Score", connectionHandle);
            playerVariables[extraid][pScore] = strval(szReturn);



Re: MySQL help - Verbal - 27.05.2012

If you're using MySQL you should do something like that:
pawn Код:
new
    playername[MAX_PLAYER_NAME],
    fquery[128];
GetPlayerName(playerid, playername, sizeof(playername));
format(fquery, sizeof(fquery), "UPDATE `users` SET `score` = '%d' WHERE `username` = '%s'", GetPlayerScore(playerid), playername);
mysql_query(fquery);



Re: MySQL help - Calgon - 27.05.2012

If you take a look at this script, it demonstrates loading and saving. You're welcome to rip the code straight from the script if you want to.


Re: MySQL help - Elysian` - 27.05.2012

Thanks calgon but that is what I already am using..

The code I said above, would that save the score already?

EDIT: I copied some other stuff inside the script for the saving and loading but I think you gotta use GetPlayerScore?


Re: MySQL help - Calgon - 27.05.2012

If you're using that now, then you can just edit the query in the saveAccount function to:

pawn Код:
format(szQuery, sizeof(szQuery), "UPDATE players SET Health = '%f', Armour = '%f', PosX = '%f', PosY = '%f', PosZ = '%f', PosR = '%f'", playerVariables[playerid][pHealth], playerVariables[playerid][pArmour], playerVariables[playerid][pPos][0], playerVariables[playerid][pPos][1], playerVariables[playerid][pPos][2], playerVariables[playerid][pPos][3]);
    format(szQuery, sizeof(szQuery), "%s, Interior = %d, Skin = %d, Score = %d WHERE ID = %d", szQuery, playerVariables[playerid][pInterior], playerVariables[playerid][pSkin], GetPlayerScore(playerid), playerVariables[playerid][pDBID]);
As you can see, 'score' is at the bottom just before the 'WHERE' clause. Score will now save by using GetPlayerScore().

You might want to use SetPlayerScore() to set the score once it's loaded though, like this:

pawn Код:
mysql_fetch_field_row(szReturn, "Score", connectionHandle);
playerVariables[extraid][pScore] = strval(szReturn);
SetPlayerScore(extraid, playerVariables[extraid][pScore]);
Otherwise, the code for loading should work - but you need to add the field to the table in MySQL, see this or many other guides on the web for how to do that.

You can post in the release thread for the script in the future if you have any more questions similar to this. I'm a lot more likely to answer them faster.


Re: MySQL help - Elysian` - 27.05.2012

Thanks cos in the saving function I used playerVariables (I dunno why) but thanks for helping me out.


Re: MySQL help - Calgon - 27.05.2012

That's the best thing to do normally, but considering that you're only going to use the score in the saving function, you might as well just use the function and forget about the player variable.

You shouldn't do that for money though, considering this.


Re: MySQL help - Elysian` - 27.05.2012

Alright, thanks.