mysql saving problem in a loop
#1

hi,

if i save my players data like this for a player on disconnect its working but not when i do it in a timer for every player.
What is wrong with this code?
Cant i use it like this in a loop for every player?
Please help, +rep!

pawn Код:
foreach(Player,i)
    {
        new query[128],pname[24];
        GetPlayerName(i, pname, 24);
        format(query, sizeof(query), "UPDATE Users SET Score=%d, Kills=%d WHERE name='%s'",GetPlayerScore(i),kills[i],pname);
        mysql_query(query);
    }
Reply
#2

Why looping throughout every player whenever a player disconnect?

OnPlayerDisconnect gets 2 parameters whenever a player quits playing in the server, the first is his ID and the second is the reason (in numbers).
Simply save the playerid that exits the server, instead of wasting resources with saving everyones!

pawn Код:
public OnPlayerDisconnect(playerid, reason)
{
        new query[128],pname[24];
        GetPlayerName(playerid, pname, 24);
        format(query, sizeof(query), "UPDATE Users SET Score=%d, Kills=%d WHERE name='%s'",GetPlayerScore(playerid),kills[playerid],pname);
        mysql_query(query);
        return 1;
}
(If you really wish to save everyone's data quickly, use a command just for that in special cases.)
Reply
#3

i said i use it for only one player when he disconnects and it works.
But i also use a timer (of 20 minutes) to save the data of all connected players once in 20 mins.
And that doesnt work. (that is the loop i posted)

Im not saving everyones data when one player disconnects^^
I did it like u showed now for one player and it works.

But the loop for every player doesnt...

Any idea?
Reply
#4

Anything came up in the mysql logs?
Reply
#5

You may be putting too much strain on the MySQL server. In my experience, it is never a good idea to execute queries in a loop, whether that be a select, insert or update statement.

Try saving when an event happens. For example OnPlayerDeath. You may set a timeout (use gettime()) so it doesn't happen every time and to protect it from fake-kill spammers. For example:

pawn Код:
public OnPlayerDeath(playerid, killerid, reason)
{
    static
        lastSave[MAX_PLAYERS];
       
    if((gettime() - lastSave[playerid]) > (5 * 60)) // 5 minutes
    {
        // Do queries
    }
   
    lastSave[playerid] = gettime();
    return 1;
}
Reply
#6

thank you so much vince!

How do i find out what is too muc strain for the mysql server?
Sometimes when many players are on server then sometimes mysql_num_rows doesnt return nothing.
That never happens when there are less players on server
May that also be due this problem?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)