MySQL Leaderboard
#1

Hello,

i got the following code.
I want to get the top 5 players ordered by score, and use them in a format. How to assign the stuff from the database to the variables "name" and "score"?

Код:
CMD:top(playerid, params[])
{
	new query[500];
	new name[5][24];
	new score[5][10];
	new num;
	mysql_format(mysql, query, sizeof(query), "SELECT name, score FROM accounts ORDER BY score DESC LIMIT 5");
	mysql_tquery(mysql, query, "");
	cache_get_field_content(0, "name", name);
	score = cache_get_field_content_int(0, "score", mysql);

	return 1;
}
Reply
#2

You must fetch the query result in a callback.

PHP код:
// command
mysql_tquery(mysqlquery"leaderboard""i"playerid);
// callback
forward leaderboard(playerid);
public 
leaderboard(playerid) {
    
// code

Reply
#3

Thanks, but still, how to assign these to the variables?
Reply
#4

This should do it just fine.

PHP код:
public leaderboard(playerid) {
    new 
rows cache_num_rows(mysql);
    new 
message[145];
    new 
name[25], score;
    for(new 
0rows; ++r) {
        
cache_get_field_content(r"name"namemysqlsizeof name);
        
score cache_get_field_content_int(r"score"mysql);
        
format(messagesizeof message"%d. %s - Score: %d", (1), namescore);
        
SendClientMessage(playerid, -1message);
    }
    return 
1;

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)