PDA

View Full Version : Houses Load


nezo2001
12/06/2015, 10:02 PM
Idk wtf is happening with me
Here is the full code I've made
#define MAX_HOUSES 1000
enum hInfo
{
HouseID,
Float:EntranceX,
Float:EntranceY,
Float:EntranceZ,
Float:ExitX,
Float:ExitY,
Float:ExitZ,
InsideInt,
owner[128],
owned,
HousePrice,
HouseText[128]
};
new HouseInfo[MAX_HOUSES][hInfo];
And I load it like this

new Cache: result = mysql_query(1, "SELECT COUNT(*) FROM houses");
printf("%i house(s) loaded", cache_get_row_int(0, 0));
cache_delete(result);
for(new x = 0; x < MAX_HOUSES; x++)
{
HouseInfo[x][HouseID] = cache_get_field_content_int(0, "id");
HouseInfo[x][EntranceX] = cache_get_field_content_float(0, "EntranceX");
HouseInfo[x][EntranceY] = cache_get_field_content_float(0, "EntranceY");
HouseInfo[x][EntranceZ] = cache_get_field_content_float(0, "EntranceZ");
HouseInfo[x][ExitX] = cache_get_field_content_float(0, "ExitX");
HouseInfo[x][ExitY] = cache_get_field_content_float(0, "ExitY");
HouseInfo[x][ExitZ] = cache_get_field_content_float(0, "ExitZ");
HouseInfo[x][InsideInt] = cache_get_field_content_int(0, "InsideInt");
cache_get_field_content(0, "owner", HouseInfo[x][owner]);
HouseInfo[x][owned] = cache_get_field_content_int(0, "owned");
HouseInfo[x][HousePrice] = cache_get_field_content_int(0, "price");
cache_get_field_content(0, "text", HouseInfo[x][HouseText]);
Create3DTextLabel(HouseInfo[x][HouseText], COLOR_GREEN, HouseInfo[x][EntranceX], HouseInfo[x][EntranceY], HouseInfo[x][EntranceZ], 30, 0, 1);
printf("Info x = %f\nInfo y = %f\nInfo z = %f",HouseInfo[x][EntranceX], HouseInfo[x][EntranceY], HouseInfo[x][EntranceZ]);
}
And it printed me

x info = 0.0000
y info = 0.0000
z info = 0.0000
x info = 0.0000
y info = 0.0000
z info = 0.0000
x info = 0.0000
y info = 0.0000
z info = 0.0000
x info = 0.0000
y info = 0.0000
z info = 0.0000
x info = 0.0000
y info = 0.0000
z info = 0.0000
x info = 0.0000
y info = 0.0000
z info = 0.0000
x info = 0.0000
y info = 0.0000
z info = 0.0000
x info = 0.0000
y info = 0.0000
z info = 0.0000
x info = 0.0000
y info = 0.0000
z info = 0.0000
//Till forever

And it isn't like that in the database

nezo2001
13/06/2015, 12:19 AM
Bumb!

nezo2001
13/06/2015, 07:40 AM
Bumb again -_-

Konstantinos
13/06/2015, 09:46 AM
You delete the result before using cache functions. Place:

cache_delete(result);

at the end (after the loop).

Also I said in your previous thread that if you want to count to use COUNT(*) but selecting all data is different ("SELECT * FROM houses").

You need to loop until rows returned (cache_get_row_count) and not until MAX_HOUSES.

In these:

cache_get_field_content_int(0, ...
cache_get_field_content_float(0, ...
cache_get_field_content(0, ...

Replace "0" with "x".

Last in cache_get_field_content and enum-array, you need to specify the length:

cache_get_field_content(x, "owner", HouseInfo[x][owner], 1, MAX_PLAYER_NAME);

and replace in your enum with:

owner[MAX_PLAYER_NAME],

nezo2001
13/06/2015, 04:52 PM
Ok but at the end what should I loop for Max_Houses or what after doing all of these :D

Konstantinos
13/06/2015, 05:05 PM
If there are let's say 5 rows and MAX_HOUSES is defined as 50, it will call the functions 45 times without having a result. That's why you loop according to the rows returned:

for (new i, j = cache_get_row_count(); i != j; ++i)

nezo2001
14/06/2015, 11:08 AM
I made it like that and nothing
new Cache: result = mysql_query(1, "SELECT * FROM houses");
for(new x, j = cache_get_row_count(); x != j; ++x)
{
HouseInfo[x][HouseID] = cache_get_field_content_int(x, "id");
HouseInfo[x][EntranceX] = cache_get_field_content_float(x, "EntranceX");
HouseInfo[x][EntranceY] = cache_get_field_content_float(x, "EntranceY");
HouseInfo[x][EntranceZ] = cache_get_field_content_float(x, "EntranceZ");
HouseInfo[x][ExitX] = cache_get_field_content_float(x, "ExitX");
HouseInfo[x][ExitY] = cache_get_field_content_float(x, "ExitY");
HouseInfo[x][ExitZ] = cache_get_field_content_float(x, "ExitZ");
HouseInfo[x][InsideInt] = cache_get_field_content_int(x, "InsideInt");
cache_get_field_content(x, "owner", HouseInfo[x][owner], 1, MAX_PLAYER_NAME);
HouseInfo[x][owned] = cache_get_field_content_int(x, "owned");
HouseInfo[x][HousePrice] = cache_get_field_content_int(x, "price");
cache_get_field_content(x, "text", HouseInfo[x][HouseText]);
Create3DTextLabel(HouseInfo[x][HouseText], COLOR_GREEN, HouseInfo[x][EntranceX], HouseInfo[x][EntranceY], HouseInfo[x][EntranceZ], 30, 0, 1);
mysql_log(LOG_ALL);
printf("x = %f\ny = %f\nz = %f",HouseInfo[x][EntranceX], HouseInfo[x][EntranceY], HouseInfo[x][EntranceZ]);
cache_delete(result);
}
}
And it printed
13:12:31] x = 1419.949951
y = -1640.239990
z = 13.546899
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
[13:12:31] x = 0.000000
y = 0.000000
z = 0.000000
//and more
And there are 148 house in the table ?!

Konstantinos
14/06/2015, 11:15 AM
How is it supposed to know how many times it will loop if you don't execute the query before?

Anyway, I see no point on using non-threaded queries as they are slower and especially when having many rows:

mysql_tquery(1, "SELECT * FROM houses", "OnHousesLoad", "");


forward OnHousesLoad();
public OnHousesLoad()
{
for(new x, j = cache_get_row_count(); x != j; ++x)
{
if (x >= sizeof HouseInfo) break; // prevent any out of bounds run time error

HouseInfo[x][HouseID] = cache_get_field_content_int(x, "id");
HouseInfo[x][EntranceX] = cache_get_field_content_float(x, "EntranceX");
HouseInfo[x][EntranceY] = cache_get_field_content_float(x, "EntranceY");
HouseInfo[x][EntranceZ] = cache_get_field_content_float(x, "EntranceZ");
HouseInfo[x][ExitX] = cache_get_field_content_float(x, "ExitX");
HouseInfo[x][ExitY] = cache_get_field_content_float(x, "ExitY");
HouseInfo[x][ExitZ] = cache_get_field_content_float(x, "ExitZ");
HouseInfo[x][InsideInt] = cache_get_field_content_int(x, "InsideInt");
cache_get_field_content(x, "owner", HouseInfo[x][owner], 1, MAX_PLAYER_NAME);
HouseInfo[x][owned] = cache_get_field_content_int(x, "owned");
HouseInfo[x][HousePrice] = cache_get_field_content_int(x, "price");
cache_get_field_content(x, "text", HouseInfo[x][HouseText], 1, /* SIZE OF HouseText HERE */);
Create3DTextLabel(HouseInfo[x][HouseText], COLOR_GREEN, HouseInfo[x][EntranceX], HouseInfo[x][EntranceY], HouseInfo[x][EntranceZ], 30, 0, 1);
}
return 1;
}


and modify this line:

cache_get_field_content(x, "text", HouseInfo[x][HouseText], 1, /* SIZE OF HouseText HERE */);

and the size you have in the enum for HouseText.

nezo2001
14/06/2015, 11:23 AM
Worked as always kon, Thanks!