Loading/saving in mysql
#1

Hi guys, I have a little request for you: would you show me how do you save and load stats with mysql ? I'm using BlueG's R33 plugin with the cache functions. So I want to see for example how do you save/load score and money, also do you keep them updated while player is playing ? (i.e. if you have /stats command which uses information from the database will it show them their current stats or the stats which were loaded when they logged in - that means their stats will be saved when they disconnect).
Reply
#2

Take a look at this include:
https://sampforum.blast.hk/showthread.php?tid=436957

It's R20, but I believe the same functions are used. If you are interested in learning, you could take a look in there.
Reply
#3

Quote:
Originally Posted by Knappen
Посмотреть сообщение
Take a look at this include:
https://sampforum.blast.hk/showthread.php?tid=436957

It's R20, but I believe the same functions are used. If you are interested in learning, you could take a look in there.
Well, this is not exactly what I needed, anyway thanks for your response!
Reply
#4

Someone else please?
Reply
#5

If you don't need full control over mysql stuff, and just overall control, take a look at latest mysql r33 release which helps nonexperienced users. https://sampforum.blast.hk/showthread.php?tid=461766 - tutorial
Reply
#6

Quote:
Originally Posted by Misiur
Посмотреть сообщение
If you don't need full control over mysql stuff, and just overall control, take a look at latest mysql r33 release which helps nonexperienced users. https://sampforum.blast.hk/showthread.php?tid=461766 - tutorial
Well, I was one of the first who saw his tut, but I dont think to use the ORM system. And the reason of mine "request" is to see different ways to load and save player data.
Reply
#7

0. Install MySQL

1. Create database, create table (I used table name "players") in it. Minimum columns:
- id (integer, AI)
- nickname (varchar, length = 24)
- money (integer, default = NULL)
- score (integer, default = NULL)
I usualy use phpmyadmin or HeidiSQL, but you can also run an SQL query from console (OS console)

2. Include MySQL plugin (R33) into server, follow instructions
3. Create global variable in your gamemode (new mysql. In OnGameModeInit add such a string:

PHP код:
mysql mysql_connect("127.0.0.1","login","database","password"); 
("connect to database and save connection handler into $mysql")

4. Insert into OnPlayerConnect (or in other function, that handle player's authorization):
PHP код:
    new query_string[128], playername[24+1];
    
GetPlayerName(playerid,playername,sizeof(playername));
    
format(query_string,sizeof(query_string),"SELECT * FROM players WHERE nickname LIKE '%s' LIMIT 0,1",playername);
    
mysql_tquery(mysqlquery_string"OnPlayerLoadFromDB","i",playerid); 
5. Add new

PHP код:
public OnPlayerLoadFromDB(playerid){
    new 
rowsfieldstemp[32];
    
cache_get_data(rowsfields);
    if(
rows != 0){
        
cache_get_field_content(0"score"temp);
        
SetPlayerScore(playeridstrval(temp));
        
cache_get_field_content(0"money"temp);
        
ResetPlayerMoney(playerid);
        
GivePlayerMoney(playeridstrval(temp));
    }

6. Insert into OnPlayerDisconnect:

PHP код:
    new query_string[128], playername[24+1];
    
GetPlayerName(playeridplayernamesizeof(playername));
    
format(query_stringsizeof(query_string), "UPDATE players SET score = %i, money = %i WHERE nickname LIKE  %s;"GetPlayerScore(playerid), GetPlayerMoney(playerid), playername);
    
mysql_tquery(mysqlquery_string"",""); 
There may be some mistakes, I didn't check this code.
EDIT: Remember to add rows into table, when new player registered - on other case, "UPDATE ..." will cause an error (as there are no row with such a nickname)
Reply
#8

Quote:
Originally Posted by therainycat
Посмотреть сообщение
4. Insert into OnPlayerConnect (or in other function, that handle player's authorization):
PHP код:
    new query_string[128], playername[24+1];
    
GetPlayerName(playerid,playername,sizeof(playername));
    
format(query_string,sizeof(query_string),"SELECT * FROM players WHERE nickname LIKE '%s' LIMIT 0,1",playername);
    
mysql_tquery(mysqlquery_string"OnPlayerLoadFromDB","i",playerid); 
5. Add new

PHP код:
public OnPlayerLoadFromDB(playerid){
    new 
rowsfieldstemp[32];
    
cache_get_data(rowsfields);
    if(
rows != 0){
        
cache_get_field_content(0"score"temp);
        
SetPlayerScore(playeridstrval(temp));
        
cache_get_field_content(0"money"temp);
        
ResetPlayerMoney(playerid);
        
GivePlayerMoney(playeridstrval(temp));
    }

6. Insert into OnPlayerDisconnect:

PHP код:
    new query_string[128], playername[24+1];
    
GetPlayerName(playeridplayernamesizeof(playername));
    
format(query_stringsizeof(query_string), "UPDATE players SET score = %i, money = %i WHERE nickname LIKE  %s;"GetPlayerScore(playerid), GetPlayerMoney(playerid), playername);
    
mysql_tquery(mysqlquery_string"",""); 
There may be some mistakes, I didn't check this code.
EDIT: Remember to add rows into table, when new player registered - on other case, "UPDATE ..." will cause an error (as there are no row with such a nickname)
This is almost the same way that I use to load the data, but not to update. I couldn't get your last sentence what rows did you mean ?
Reply
#9

I meant, you must create new row with player's nickname, when new player registers on your server.
Previously, check if that player exists in database.

At last, it looks like an authorization system:
when player connects, check whether player is already exists in table "players"
if so, just load his stats (I wrote about it in my last message)
if not, create new row ("INSERT INTO players ...") and set score & money to default valuses
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)