MySQL Registered Players!
#1

How to get a total amount of Players with mySQL I want it to say how many Registered players the server got by typing:

CMDlayers

The table I use is: Accounts
Reply
#2

pawn Код:
CMD:players(playerid, params[])
Reply
#3

Query:
pawn Код:
SELECT ID FROM Accounts
Code:
pawn Код:
new NumPlayers = mysql_num_rows();
printf("Registered players: %i", NumPlayers);
Reply
#4

Could you do the full cmd?
Reply
#5

pawn Код:
// This command "/registeredplayers" displays the amount of registered players
COMMAND:registeredplayers(playerid, params[])
{
    // Setup local variables
    new NumPlayers, Msg[128];

    // Execute the query to get all ID's from all accounts in table "Accounts"
    mysql_query("SELECT ID FROM Accounts");
    // Store the result
    mysql_store_result();
    // Count the amount of rows returned in the result
    NumPlayers = mysql_num_rows();
    // Free the result
    mysql_free_result();

    format(Msg, sizeof(Msg), "Registered players: %i", NumPlayers);
    SendClientMessage(playerid, -1, Msg);

    // Let the server know that this was a valid command
    return 1;
}
Something like this should work.
Reply
#6

The proper way to do this is actually the COUNT() function, not mysql_num_rows.
Reply
#7

Hi,

Is there a way to count total registred players with threaded queries? Or should I use this ?
If I want to use this but for a textdraw, will it be the same ?


Thank you.
Reply
#8

Threaded queries need callback. You can use y_inline to keep it inside one function:

pawn Код:
CMD:registeredplayers(playerid, params[])
{
    inline FetchResults() {
        //Result is always present
        new
            numberOfPlayers = cache_get_row_int(0, 0, dbhandle)
        ;

        //Do something with numberOfPlayers
    }

    mysql_pquery_inline(dbhandle, "SELECT COUNT(id) FROM Accounts", using inline FetchResults);
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 7 Guest(s)