SA-MP Forums Archive
Get rank direct from mysql - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Get rank direct from mysql (/showthread.php?tid=566935)



Get rank direct from mysql - ajwar - 09.03.2015

Is it possible to get players rank by Score direct from database?

I mean sql function that would return 2 ID rank as #1, 1 ID rank as #2...

example: GetIDrank(2); would return 1;
example1: GetIDrank(0); would return 3;

ID Score
0 5
1 6
2 8
3 1


Re: Get rank direct from mysql - Misiur - 09.03.2015

Ugly, unthreaded function:
pawn Код:
GetIDRank(id)
{
    new
        query[128],
        value
    ;

    mysql_format(handle, query, sizeof query, "SELECT COUNT(id) AS rank FROM <tablename> WHERE Score >= (SELECT Score FROM <tablename> WHERE id = %d)", id);
    new Cache:result = mysql_query(MySQL, query);
    value = cache_get_row_count(handle) ? cache_get_field_content_int(0, "rank", handle) : -1;
    cache_delete(result);

    return value;
}
Don't call this function often, unthreaded queries are really rarely necessary. Use inline handling if you want to keep your logic together, but still use threaded version