PDA

View Full Version : SQL error


Bondage
21/01/2015, 07:08 PM
mysql_format(mysql, query, sizeof(query), "SELECT `user`, `hours`, `minutes` FROM `players` WHERE `banned`=0 ORDER BY `hours` DESC LIMIT 10");

Above shown is a code to display the top 10 online members but it is showing until 9 rows, the 10th one says this in the server

NULL with 0 hours and 0 minutes

here is the mysql log shows from 9th row (9th row is fine, just to show how it works until 9) but the problem starts with 10 as you could see below.



[00:45:56] [DEBUG] cache_get_field_content - row: 9, field_name: "user", connection: 1, max_len: 24
[00:45:56] [DEBUG] CMySQLResult::GetRowDataByName - row: '9', field: "user", data: "Jason
"
[00:45:56] [DEBUG] cache_get_field_content_int - row: 9, field_name: "hours", connection: 1
[00:45:56] [DEBUG] CMySQLResult::GetRowDataByName - row: '9', field: "hours", data: "707"

[00:45:56] [DEBUG] cache_get_field_content_int - row: 9, field_name: "minutes", connection: 1
[00:45:56] [DEBUG] CMySQLResult::GetRowDataByName - row: '9', field: "minutes", data: "20"

//----------------------ERROR starts from here on

[00:45:56] [DEBUG] cache_get_field_content - row: 10, field_name: "user", connection: 1, max_len: 24
[00:45:56] [ERROR] CMySQLResult::GetRowDataByName() - invalid row index ('10')
[00:45:56] [DEBUG] cache_get_field_content_int - row: 10, field_name: "hours", connection: 1
[00:45:56] [ERROR] CMySQLResult::GetRowDataByName() - invalid row index ('10')
[00:45:56] [ERROR] cache_get_field_content_int - invalid datatype
[00:45:56] [DEBUG] cache_get_field_content_int - row: 10, field_name: "minutes", connection: 1
[00:45:56] [ERROR] CMySQLResult::GetRowDataByName() - invalid row index ('10')
[00:45:56] [ERROR] cache_get_field_content_int - invalid datatype

PowerPC603
21/01/2015, 07:11 PM
Returning 10 rows gives you row 0 to row 9.
There is no row 10, you're trying to get data where none exists.

Bondage
21/01/2015, 07:20 PM
Returning 10 rows gives you row 0 to row 9.
There is no row 10, you're trying to get data where none exists.

I've tried it with " DESC LIMIT 11 " and it gave me 10 outputs but the error shows in the mysql log. After all i've been using the same code for my R5 gamemode and then it was working fine.

PowerPC603
21/01/2015, 09:23 PM
Even though you're searching for up to 10 rows, you may not get 10 rows everytime.
It's best to check the amount of rows returned from your query and loop through them.
That will prevent you accessing non-existing data/rows.

The error is pretty clear: invalid row-index.
This means row 10 doesn't exist in the result.

It's the same as accessing an array beyond it's limits.

Just try a pretty standard query like

SELECT * FROM players WHERE Name = 'Test' LIMIT 1

And try accessing row 200 or something really outside the range of your query.
You should get the same error.

Bondage
21/01/2015, 11:55 PM
So that means errors would be still there while executing this code?