ORDER BY SQL
#1

hi, I have the following data on the table

id=0, kills=88
id=1, kills=90
id=2, kills=99
id=3, kills=110
id=4, kills=120
id=5, kills=3300
id=6, kills=789
id=7, kills=55
id=8, kills=33

I sort data
PHP код:
new
    
db_countkills [30], idnexid [30];
sys_result db_query (sys_db"SELECT * FROM `players` ORDER BY `kills` DESC");
db_count db_num_rows(sys_result);
for (new 
!= db_count; ++i)
{
    
db_get_field_assoc (sys_result"kills"sys_field20),
        
kills [idnex++] = strval(sys_field);
    
db_get_field_assoc (sys_result"id"sys_field20),
        
id [idnex] = strval(sys_field);
    
db_next_row (sys_result);
}
db_free_result (sys_result);
for (new 
!= 9; ++iprintf ("id %i, kills %i"id[i], kills[i]); 
That's what happens
Quote:

[22:20:03] id 0, kills 99
[22:20:03] id 3, kills 90
[22:20:03] id 2, kills 88
[22:20:03] id 1, kills 789
[22:20:03] id 7, kills 55
[22:20:03] id 8, kills 3300
[22:20:03] id 6, kills 33
[22:20:03] id 9, kills 120
[22:20:03] id 5, kills 110

must be
Quote:

[22:20:03] id 8, kills 3300
[22:20:03] id 1, kills 789
[22:20:03] id 9, kills 120
[22:20:03] id 5, kills 110
[22:20:03] id 0, kills 99
[22:20:03] id 3, kills 90
[22:20:03] id 2, kills 88
[22:20:03] id 7, kills 55
[22:20:03] id 6, kills 33

it is fixable?
sorry my bad english=)
Reply
#2

Makes me believe that you are saving the value as text or varchar, rather than integer.
Reply
#3

Quote:
Originally Posted by Vince
Посмотреть сообщение
Makes me believe that you are saving the value as text or varchar, rather than integer.
you are right, thank you, I forgot to specify the type INTEGER when creating a table=)
Reply
#4

The idnex (index*) is wrong I think too. It will start: kill[0] but id[1] instead of 0. Make sure about the datatype as Vince said and then try:
pawn Код:
db_get_field_assoc (sys_result, "kills", sys_field, 20),
    kills [i] = strval(sys_field);
db_get_field_assoc (sys_result, "id", sys_field, 20),
    id [i] = strval(sys_field);
db_next_row (sys_result);
it works fine to me.

pawn Код:
// Output:
[15:48:06] rows: 9
[15:48:06] id 5, kills 3300
[15:48:06] id 6, kills 789
[15:48:06] id 4, kills 120
[15:48:06] id 3, kills 110
[15:48:06] id 2, kills 99
[15:48:06] id 1, kills 90
[15:48:06] id 0, kills 88
[15:48:06] id 7, kills 55
[15:48:06] id 8, kills 33
By the way, use LIMIT clause in the query because if you get more than 30 rows, it will exceed the array's indexes (out of bounds).

pawn Код:
sys_result = db_query (Database, "SELECT * FROM `players` ORDER BY `kills` DESC LIMIT 30"); // change to the top N you want.
Reply
#5

all of the code for an example only, I realized my mistake, thank you.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)