Top x players using sqlite problem
#1

Hi, i have problem. The command runs perfectly but the result is not as expected, the sample exp as 0

pawn Код:
CMD:topstat(playerid,params[])
{
    new string[128], query[256];
    SendClientMessage(playerid,COLOR_GREEN,"Top X Y'ers");
    new string2[128];
    new DBResult:result;
    new DBResult:result2;
    format(query,sizeof(query),"SELECT `Username` FROM `users` ORDER BY (`EXP` * 1) DESC limit 5");
/*Select from the `Name` column in the table `Users`, and order them by stat on a descreasing list limited to the top 3
I had ('stat` *1) because without it it'll look at it as if it's a string, timing it by 1 will turn it into a number.*/

    result = db_query(Database,query);
    for(new a;a<db_num_rows(result);a++)//what we have now is 10 rows in the database result, so we'll do a loop to show each one on a new line.
    {
        db_get_field(result,0,string,128);//get the result of the database and format it into a string
//at the moment this will return the first row, we need to switch it using a function.
        format(query,sizeof(query),"SELECT `EXP` FROM `users` WHERE `Username` = lower('%s')", string);
/*this result2 will get the data from the stat column from the row that matches the first row of the "result" variable
"result" is already in order from 1-10, "result" will be reading the 1st row at the moment or the player with the highest stat*/

        result2 = db_query(Database,query);
        db_get_field(result2,0,string2,128);//get the `stat` column from the row of the first player.
        format(string,sizeof(string),"%d. %s Stat: %d",a+1,string,string2);//it's a+1 because the result starts at 0, this will display as "1. Name Stat: X".
        SendClientMessage(playerid,COLOR_ORANGE,string);
        db_next_row(result);//this function changes to the next row down, then when it goes into the next loop cycle it'll get the second top player and so forth.
        db_free_result(result2);//remember to free any results you don't need anymore
    }
    db_free_result(result);
    return 1;
}
In DB i register:

Player, EXP = 10
Player2, EXP = 8
Player3, EXP = 7
Player4, EXP = 5
Player5, EXP = 2

I get in the right order

1. Player Stat: 0
2. Player2 Stat: 0
3. Player3 Stat: 0
4. Player4 Stat: 0
5. Player5 Stat: 0

No show player exp, only gets 0
Reply
#2

SMALL BUMP
Reply
#3

pawn Код:
CMD:topstat(playerid,params[])
{
    SendClientMessage(playerid,COLOR_GREEN,"Top X Y'ers");
    new DBResult:result, a, string[128], exp[20];
    format(string,sizeof(string),"SELECT `Username`, `EXP` FROM `users` ORDER BY `EXP` DESC LIMIT 5");
    result = db_query(Database,string);
    if(db_num_rows(result))
    {
        do
        {
            db_get_field(result,0,string,25);
            db_get_field(result,1,exp,sizeof(exp));
            format(string,sizeof(string),"%d. %s Stat: %s",++a,string,exp);
            SendClientMessage(playerid,COLOR_ORANGE,string);
        }
        while(db_next_row(result));
    }
    db_free_result(result);
    return 1;
}
Reply
#4

Quote:
Originally Posted by Jefff
Посмотреть сообщение
pawn Код:
CMD:topstat(playerid,params[])
{
    SendClientMessage(playerid,COLOR_GREEN,"Top X Y'ers");
    new DBResult:result, a, string[128], exp[20];
    format(string,sizeof(string),"SELECT `Username`, `EXP` FROM `users` ORDER BY `EXP` DESC LIMIT 5");
    result = db_query(Database,string);
    if(db_num_rows(result))
    {
        do
        {
            db_get_field(result,0,string,25);
            db_get_field(result,1,exp,sizeof(exp));
            format(string,sizeof(string),"%d. %s Stat: %s",++a,string,exp);
            SendClientMessage(playerid,COLOR_ORANGE,string);
        }
        while(db_next_row(result));
    }
    db_free_result(result);
    return 1;
}
Thanks friend, but I had to do this to give me the top correctly

pawn Код:
format(string,sizeof(string),"SELECT `Username`, `EXP` FROM `users` ORDER BY (`EXP` * 1) DESC LIMIT 5");
I had ('EXP` *1) because without it it'll look at it as if it's a string, timing it by 1 will turn it into a number.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)