SA-MP Forums Archive
GetFreeHouseID 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: GetFreeHouseID Mysql (/showthread.php?tid=478021)



GetFreeHouseID Mysql - Admigo - 27.11.2013

Hello,

I have a problem with my mysql house system.
I am trying to get the next free house id.
But when i use this stock its returning -1 while its needs to return 2 because i am already have houseid 1 in the database.
Код:
stock GetFreeHouseID()
{
    for(new h = 0; h < MAX_HOUSES; h++)
    {
		new query[128];
		format(query, sizeof(query), "SELECT * FROM `"#MYSQL_HOUSE_TABLE2"` WHERE `HouseID` = '%d'", h);
		mysql_store_result();
		if(mysql_num_rows(gSQL) == 0)
		{
			return h;
		}
        mysql_free_result();
	}
	return -1;
}
How can i fix this?

Admigo


Re: GetFreeHouseID Mysql - Kyle - 27.11.2013

This won't help here, but you should be using threaded queries.


Re: GetFreeHouseID Mysql - Konstantinos - 27.11.2013

Don't use the WHERE clause and neither the loop so it can select all the rows and then get the rows with the function is used for that.

Quote:
Originally Posted by KyleSmith
Посмотреть сообщение
This won't help here, but you should be using threaded queries.
It's indeed better of using threaded queries.


Re: GetFreeHouseID Mysql - Admigo - 27.11.2013

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Don't use the WHERE clause and neither the loop so it can select all the rows and then get the rows with the function is used for that.



It's indeed better of using threaded queries.
Okay thanks for the information. I will use threaded queries.


Re: GetFreeHouseID Mysql - Jefff - 27.11.2013

Use house arrays for this, example

pawn Код:
stock GetFreeHouseID()
{
    for(new h = 0; h < MAX_HOUSES; h++)
    {
        if(HouseInfo[h][hID] == 0) // or if house is not owned
            return h;
    }
    return -1;
}
or load in OnGameModeInit all houses and set
HousesInGame = LoadedHouses;

and your free slot is HousesInGame
pawn Код:
HouseInfo[HousesInGame][hID] = HousesInGame;
rest house arrays
...
...
HousesInGame++;