Problem with loading houses
#1

Hi, I've attempted to make a MySQL housing system.
This is my LoadHouses function
pawn Код:
public LoadHouses()
{
    for(new i=1; i<MAX_HOUSES; i++)
    {
        format(gQuery,sizeof gQuery,"SELECT * FROM `"Houses_Table"` WHERE `HouseID` = '%i'",i);
        mysql_query(gQuery);
        mysql_store_result();
        if(mysql_num_rows() == 1)
        {
            mysql_fetch_row_format(gQuery);
            sscanf(gQuery,"e<p<|>iiis[25]iiffffff>",HouseInfo[i]);
            mysql_free_result();
            new lString[256];
            switch(HouseInfo[i][hOwned])
            {
                case 0:
                {
                   CreateDynamicMapIcon(HouseInfo[i][OutX], HouseInfo[i][OutY], HouseInfo[i][OutZ], 31, -1);
                   format(lString,sizeof(lString),"House Owned: No\nHouse Price: $%i\n",HouseInfo[i][hPrice]);
                   HouseInfo[i][Label] = CreateDynamic3DTextLabel(lString,COLOR_RED,HouseInfo[i][OutX],HouseInfo[i][OutY],HouseInfo[i][OutZ]+1.5,50.0);
                   HouseInfo[i][HouseEnterCP] = CreateDynamicCP(HouseInfo[i][OutX],HouseInfo[i][OutY],HouseInfo[i][OutZ],1.5,-1,-1,-1,50.0);
                }
                case 1:
                {
                    CreateDynamicMapIcon(HouseInfo[i][OutX], HouseInfo[i][OutY], HouseInfo[i][OutZ], 32, -1);
                    format(lString,sizeof(lString),"House Owned: Yes\nHouse Owner: %s\nHouse Price: $%i\n",HouseInfo[i][hOwner],HouseInfo[i][hPrice]);
                    HouseInfo[i][Label] = CreateDynamic3DTextLabel(lString,COLOR_RED,HouseInfo[i][OutX],HouseInfo[i][OutY],HouseInfo[i][OutZ]+1.5,50.0);
                    HouseInfo[i][HouseEnterCP] = CreateDynamicCP(HouseInfo[i][OutX],HouseInfo[i][OutY],HouseInfo[i][OutZ],1.5,-1,-1,-1,50.0);
                    HouseInfo[i][HouseExitCP] = CreateDynamicCP(HouseInfo[i][IntX],HouseInfo[i][IntY],HouseInfo[i][IntZ],1.5,-1,-1,-1,20.0);
                }
            }
            HousesCreated++;
        }
    }
    mysql_free_result();
    printf("The server has retrieved %i houses from the database!",HousesCreated);
    return 1;
}
For testing, I created 1 house ingame. When I tried tried to retrieve the houses, It retrieved the first house. That was good. But, It started to check for unknown houses. The table only has 1 house. But, It looks till 500 houses.
It's spamming my mysql_log.txt.
Reply
#2

That's because you loop 500 times.

Execute only 1 query:
pawn Код:
"SELECT * FROM "Houses_Table""
and then loop as many times as the rows are. Use an if statement inside the loop that checks if the i is equal to the size of HouseInfo array and use break; to stop the loop so it won't go out of bounds in case there are more houses in the database than you've declared to the array.

PS: The index should start from 0 and not from 1.
PS2: Updating the mysql plugin to R38 and using threaded queries which are faster and better is recommended.
Reply
#3

It worked! Thanks mate! (:
Thanks for the suggestion as well.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)