mySQL top 10 -
MP2 - 18.07.2011
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.
Re: mySQL top 10 -
Lorenc_ - 18.07.2011
TKAdmin has an example, check it out. It includes the page.
Re: mySQL top 10 -
MP2 - 18.07.2011
Looks to be that's a website panel. I need this in samp.
Re: mySQL top 10 -
Calgon - 18.07.2011
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.
Re: mySQL top 10 -
MP2 - 18.07.2011
So will that send 10 queries?
Re: mySQL top 10 -
Calgon - 18.07.2011
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.
Re: mySQL top 10 -
MP2 - 18.07.2011
Okay, thanks. Could you just explain what sccanf does in this code? I don't use it.
Re: mySQL top 10 -
Calgon - 18.07.2011
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:
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.
Re: mySQL top 10 -
MP2 - 18.07.2011
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?
Re: mySQL top 10 -
Calgon - 18.07.2011
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.