mySQL top 10
#1

I've only been using mySQL for a few months on my server, so I only really know the basics. Could anyone point me in the right direction on how to display a top ten leaderboard (for example 'Score' in the table 'players') in a textdraw?

Thanks in advance.
Reply
#2

TKAdmin has an example, check it out. It includes the page.
Reply
#3

Looks to be that's a website panel. I need this in samp.
Reply
#4

pawn Code:
mysql_query("SELECT `Name`, `Score` FROM `players` ORDER BY `Score` DESC LIMIT 10");
mysql_store_result();
new szPrepTD[460], iIterator = 1, iScore, szName[MAX_PLAYER_NAME], szUnload[64];
while(mysql_fetch_row_format(szUnload)) {
// This code will repeat ten times.
sscanf(szUnload, "p<|>sd", szName, iScore);
if(strlen(szPrepTD) == 0) format(szPrepTD, sizeof(szPrepTD), "#%d - %s (%d score)\n", iIterator, szName, iScore);
format(szPrepTD, sizeof(szPrepTD), "%s#%d - %s (%d score)\n", szPrepTD, iIterator, szName, iScore);
iIterator++;
}
mysql_free_result();
Use 'szPrepTD' as the string to set your textdraw. Something like this will work, I haven't tested this though. Rename fields as necessary and whatnot.
Reply
#5

So will that send 10 queries?
Reply
#6

No, there is only one query. But 10 records are stored in memory, and the while() code retrieves the results from memory in to the code within the while code block.
Reply
#7

Okay, thanks. Could you just explain what sccanf does in this code? I don't use it.
Reply
#8

I was too lazy to use a split function in this example, but because mysql_fetch_row_format has multiple results to return, it returns them in this format:

Code:
Player_Name|4
The sscanf splits that string in to a string and an integer so you can use iScore and szName. It splits it by the '|' (vertical pipe) character.

I just edited the code now because I missed something in the sscanf code.
Reply
#9

When I use it, I get spam in my logs:

[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.

How do I fix this?
Reply
#10

pawn Code:
sscanf(szUnload, "p<|>s[24]d", szName, iScore);
Wasn't really thinking when I wrote the code originally, sorry. Try the code above.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)