Why is this not loading MySQL data into the variables properly?
#1

pawn Код:
stock LoadHouses()
{
    mysql_function_query(g_Handle, "SELECT * FROM `Houses`", true, "HousesLoad", "");
    return 1;
}
pawn Код:
public HousesLoad()
{
    new temp[400], string[400], rows, fields;
    cache_get_data(rows, fields, g_Handle);

    if(rows)
    {
        for(new id = 0; id < rows; id++)
        {
            cache_get_row(0, 0, temp, g_Handle), HouseInfo[id][hHouseID] = strval(temp);
            cache_get_row(0, 1, temp, g_Handle), HouseInfo[id][hAddress] = strval(temp);
            cache_get_row(0, 2, temp, g_Handle), HouseInfo[id][hPrice] = strval(temp);
            cache_get_row(0, 3, temp, g_Handle), HouseInfo[id][hEnterPos][0] = floatstr(temp);
            cache_get_row(0, 4, temp, g_Handle), HouseInfo[id][hEnterPos][1] = floatstr(temp);
            cache_get_row(0, 5, temp, g_Handle), HouseInfo[id][hEnterPos][2] = floatstr(temp);
            cache_get_row(0, 6, temp, g_Handle), HouseInfo[id][hExitPos][0] = floatstr(temp);
            cache_get_row(0, 7, temp, g_Handle), HouseInfo[id][hExitPos][1] = floatstr(temp);
            cache_get_row(0, 8, temp, g_Handle), HouseInfo[id][hExitPos][2] = floatstr(temp);

            HouseInfo[id][hPickup] = CreateDynamicPickup(1273, 23, HouseInfo[id][hEnterPos][0], HouseInfo[id][hEnterPos][1], HouseInfo[id][hEnterPos][2], -1, -1, -1, 100.0);
            format(string,sizeof(string),"HouseID: %d\nAddress: %s\nPrice: %d", HouseInfo[id][hHouseID], HouseInfo[id][hAddress], HouseInfo[id][hPrice]);
            HouseInfo[id][hText] = CreateDynamic3DTextLabel(string, -1, HouseInfo[id][hEnterPos][0], HouseInfo[id][hEnterPos][1], HouseInfo[id][hEnterPos][2], 100, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, -1, -1, -1, 100);
            break;
        }
        printf("%i houses loaded.", rows);
    }
    return 1;
}
It loads the rows from the MySQL database and adds the pickups and text labels, but the address information from the text labels are blank?

If I do this in the loop, it says "0 houses."... Been stuck on this for days, doesn't make sense to me.

pawn Код:
printf("%i houses.", id);
Reply
#2

Lol.. Bump :/
Reply
#3

Still don't understand this..
Reply
#4

Adress is string so don't use strval for it:
pawn Код:
cache_get_row(0, 1, HouseInfo[id][hAddress], g_Handle, /* SIZE OF hAdress HERE */);
Also a suggestion: it would be better if you used a while loop so you can check if the rows are not more than the size of HouseInfo. Just to prevent a run time error 4 in case that was about to happen.
Reply
#5

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Adress is string so don't use strval for it:
pawn Код:
cache_get_row(0, 1, HouseInfo[id][hAddress], g_Handle, /* SIZE OF hAdress HERE */);
Also a suggestion: it would be better if you used a while loop so you can check if the rows are not more than the size of HouseInfo. Just to prevent a run time error 4 in case that was about to happen.
Thanks, it works except it only loads the first house in the database row. I've used a debug and it shows that two houses are called from the database, but it only shows the pickup and text label for the first one.

pawn Код:
public HousesLoad()
{
    new temp[400], string[400], rows, fields;
    cache_get_data(rows, fields, g_Handle);

    if(rows)
    {
        for(new id = 0; id < MAX_HOUSES; id++)
        {
            cache_get_row(0, 0, temp, g_Handle), HouseInfo[id][hHouseID] = strval(temp);
            cache_get_row(0, 1, HouseInfo[id][hAddress], g_Handle, 31);
            cache_get_row(0, 2, temp, g_Handle), HouseInfo[id][hPrice] = strval(temp);
            cache_get_row(0, 3, temp, g_Handle), HouseInfo[id][hEnterPos][0] = floatstr(temp);
            cache_get_row(0, 4, temp, g_Handle), HouseInfo[id][hEnterPos][1] = floatstr(temp);
            cache_get_row(0, 5, temp, g_Handle), HouseInfo[id][hEnterPos][2] = floatstr(temp);
            cache_get_row(0, 6, temp, g_Handle), HouseInfo[id][hExitPos][0] = floatstr(temp);
            cache_get_row(0, 7, temp, g_Handle), HouseInfo[id][hExitPos][1] = floatstr(temp);
            cache_get_row(0, 8, temp, g_Handle), HouseInfo[id][hExitPos][2] = floatstr(temp);

            HouseInfo[id][hPickup] = CreateDynamicPickup(1273, 23, HouseInfo[id][hEnterPos][0], HouseInfo[id][hEnterPos][1], HouseInfo[id][hEnterPos][2], -1, -1, -1, 100.0);
            format(string,sizeof(string),"HouseID: %d\nAddress: %s\nPrice: %d", HouseInfo[id][hHouseID], HouseInfo[id][hAddress], HouseInfo[id][hPrice]);
            HouseInfo[id][hText] = CreateDynamic3DTextLabel(string, -1, HouseInfo[id][hEnterPos][0], HouseInfo[id][hEnterPos][1], HouseInfo[id][hEnterPos][2], 100, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, -1, -1, -1, 100);
            break;
        }
        printf("%i houses loaded.", rows); // This shows that two houses have been loaded.
    }
    return 1;
}
Also, I'm using the up to date MySQL plugin so not sure what while loop I could use?
Thanks. Giving me headache been stuck on this for over a week but managed to code more complicated things and get them working
Reply
#6

You're always loading from row 0 and thus putting the same data across your entire array. Use id instead of 0 in cache_get_row.
Reply
#7

Changed it but still loads only the first house.
Here is my whole housing system.

pawn Код:
#define MAX_HOUSES          200
pawn Код:
enum hInfo
{
    hHouseID,
    hAddress[32],
    hPrice,
    hPickup,
    Text3D:hText,
    Float:hEnterPos[3],
    Float:hExitPos[3]
}

new HouseInfo[MAX_HOUSES][hInfo], houseid;
pawn Код:
public OnGameModeInit()
{
    LoadHouses();
}
pawn Код:
stock CreateHouse(price, Float:PosX, Float:PosY, Float:PosZ)
{
    new string[128], query[124];

    HouseInfo[houseid][hHouseID] = houseid;
    HouseInfo[houseid][hEnterPos][0] = PosX;
    HouseInfo[houseid][hEnterPos][1] = PosY;
    HouseInfo[houseid][hEnterPos][2] = PosZ;
    HouseInfo[houseid][hExitPos][0] = PosX;
    HouseInfo[houseid][hExitPos][1] = PosY;
    HouseInfo[houseid][hExitPos][2] = PosZ;
    format(HouseInfo[houseid][hAddress], 35, "1 San Fierro Drive");
    HouseInfo[houseid][hPrice] = price;

    format(query, sizeof(query), "INSERT INTO `Houses` (`HouseID`, `Address`, `Price`, `EnterX`, `EnterY`, `EnterZ`, `ExitX`, `ExitY`, `ExitZ`) VALUES (%d, \'%s\', %d, %f, %f, %f, %f, %f, %f)",
    HouseInfo[houseid][hHouseID] = houseid,
    HouseInfo[houseid][hAddress],
    HouseInfo[houseid][hPrice],
    HouseInfo[houseid][hEnterPos][0],
    HouseInfo[houseid][hEnterPos][1],
    HouseInfo[houseid][hEnterPos][2],
    HouseInfo[houseid][hExitPos][0],
    HouseInfo[houseid][hExitPos][1],
    HouseInfo[houseid][hExitPos][2]
    );

    mysql_function_query(g_Handle, query, false, "", "");

    HouseInfo[houseid][hPickup] = CreateDynamicPickup(1273, 23, PosX, PosY, PosZ, -1, -1, -1, 100.0);
    format(string,sizeof(string),"HouseID: %d\nAddress: %s\nPrice: %d", HouseInfo[houseid][hHouseID], HouseInfo[houseid][hAddress], HouseInfo[houseid][hPrice]);
    HouseInfo[houseid][hText] = CreateDynamic3DTextLabel(string, -1, PosX, PosY, PosZ, 100, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, -1, -1, -1, 100);
    houseid ++;
}
pawn Код:
CMD:createhouse(playerid, params[])
{
    new Float:pos[3], string[30], price;
    if(!IsPlayerLoggedIn(playerid))
    {
        SendClientMessage(playerid, -1, "You are restricted from using commands until you log in.");
        return 1;
    }
    if(PlayerInfo[playerid][pAdminLevel] < 1)
    {
        SendClientMessage(playerid, -1, "You do not have the authority to use this command.");
        return 1;
    }
    if(!AdminDuty[playerid])
    {
        SendClientMessage(playerid, -1, "You are not on duty as an Administrator (/aduty).");
        return 1;
    }
    if(sscanf(params, "i", price))
    {
        SendClientMessage(playerid, -1, "Usage: /createhouse <price>");
        return 1;
    }
    GetPlayerPos(playerid, pos[0], pos[1], pos[2]);
    CreateHouse(price, pos[0], pos[1], pos[2]);
    format(string, sizeof(string), "You have created House ID %d.", houseid);
    SendClientMessage(playerid, -1, string);
    return 1;
}
pawn Код:
stock LoadHouses()
{
    mysql_function_query(g_Handle, "SELECT * FROM `Houses`", true, "HousesLoad", "");
    return 1;
}
pawn Код:
forward HousesLoad();
public HousesLoad()
{
    new temp[400], string[120], rows, fields;
    cache_get_data(rows, fields, g_Handle);

    if(rows)
    {
        for(new id = 0; id < MAX_HOUSES; id++)
        {
            HouseInfo[id][hHouseID] = cache_get_row_int(id, 0, g_Handle);
            cache_get_row(id, 1, HouseInfo[houseid][hAddress], g_Handle, 31);
            cache_get_row(id, 2, temp, g_Handle), HouseInfo[id][hPrice] = strval(temp);
            cache_get_row(id, 3, temp, g_Handle), HouseInfo[id][hEnterPos][0] = floatstr(temp);
            cache_get_row(id, 4, temp, g_Handle), HouseInfo[id][hEnterPos][1] = floatstr(temp);
            cache_get_row(id, 5, temp, g_Handle), HouseInfo[id][hEnterPos][2] = floatstr(temp);
            cache_get_row(id, 6, temp, g_Handle), HouseInfo[id][hExitPos][0] = floatstr(temp);
            cache_get_row(id, 7, temp, g_Handle), HouseInfo[id][hExitPos][1] = floatstr(temp);
            cache_get_row(id, 8, temp, g_Handle), HouseInfo[id][hExitPos][2] = floatstr(temp);

            HouseInfo[id][hPickup] = CreateDynamicPickup(1273, 23, HouseInfo[id][hEnterPos][0], HouseInfo[id][hEnterPos][1], HouseInfo[id][hEnterPos][2], -1, -1, -1, 100.0);
            format(string,sizeof(string),"HouseID: %d\nAddress: %s\nPrice: %d", HouseInfo[id][hHouseID], HouseInfo[id][hAddress], HouseInfo[id][hPrice]);
            HouseInfo[id][hText] = CreateDynamic3DTextLabel(string, -1, HouseInfo[id][hEnterPos][0], HouseInfo[id][hEnterPos][1], HouseInfo[id][hEnterPos][2], 100, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, -1, -1, -1, 100);
            break;
        }
    }
    return 1;
}
Database layout for the 'Houses' table (only creates ID 0 pickup and text label on game mode start):

Reply
#8

Oh, duh. Remove the break. How could I have missed such blatant mistake.
Reply
#9

Thanks Vince. What is 'break' used for? I thought it stops the loop running until the max houses?
Reply
#10

It stops the loop entirely. It's similar to return except that break will transfer code flow to code that may be below the loop, whereas return will end the function entirely.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)