Loading all data from the table
#1

hello, i tried a query to load all the houses on OnGameModeInit with this query:

Код:
SELECT * FROM `house`
and this is the code to actually process the query and make the houses:

pawn Код:
public OnHousesLoaded()
{
    new rows, fields;
    cache_get_data(rows, fields, Gconnection);

    if(rows) {

        new owner[24], Intname[100], X[50], Y[50], Z[50], SQL[50], create;
        cache_get_row(0, 1, owner);
        cache_get_row(0, 3, Intname);
        cache_get_row(0, 5, X);
        cache_get_row(0, 6, Y);
        cache_get_row(0, 7, Z);
        cache_get_row(0, 0, SQL);

        create = HouseCreate(owner, Intname, floatstr(X), floatstr(Y), floatstr(Z));
        HouseInfo[create][SqlID] = strval(SQL);
        print(" it's 'loaded'");

        printf("%s %s %f %f %f %d", owner, Intname, floatstr(X), floatstr(Y), floatstr(Z), strval(SQL));
    }
    return 1;
}
It only loads the first row in the database.. I tried using a while loop but then it spammed loading the same thing.
Reply
#2

You need to free the result at the end of your query.
Reply
#3

R7 automatically free's the result. Also adding it in manually does no change.
Reply
#4

pawn Код:
public OnHousesLoaded()
{
    new rows, fields;
    cache_get_data(rows, fields, Gconnection);

    if(rows) {
        for(new i = 0; i < rows; i++) {
        new owner[24], Intname[100], X[50], Y[50], Z[50], SQL[50], create;
        cache_get_row(i, 1, owner);
        cache_get_row(i, 3, Intname);
        cache_get_row(i, 5, X);
        cache_get_row(i, 6, Y);
        cache_get_row(i, 7, Z);
        cache_get_row(i, 0, SQL);

        create = HouseCreate(owner, Intname, floatstr(X), floatstr(Y), floatstr(Z));
        HouseInfo[create][SqlID] = strval(SQL);
        print(" it's 'loaded'");

        printf("%s %s %f %f %f %d", owner, Intname, floatstr(X), floatstr(Y), floatstr(Z), strval(SQL));
        }
    }
    return 1;
}
rows gives you the number of rows back. So you can loop through them with a for loop, or a while, but you have to keep track of which rows you have done already.
Reply
#5

Too late.

--
Reply
#6

Quote:
Originally Posted by mamorunl
Посмотреть сообщение
pawn Код:
public OnHousesLoaded()
{
    new rows, fields;
    cache_get_data(rows, fields, Gconnection);

    if(rows) {
        for(new i = 0; i < rows; i++) {
        new owner[24], Intname[100], X[50], Y[50], Z[50], SQL[50], create;
        cache_get_row(i, 1, owner);
        cache_get_row(i, 3, Intname);
        cache_get_row(i, 5, X);
        cache_get_row(i, 6, Y);
        cache_get_row(i, 7, Z);
        cache_get_row(i, 0, SQL);

        create = HouseCreate(owner, Intname, floatstr(X), floatstr(Y), floatstr(Z));
        HouseInfo[create][SqlID] = strval(SQL);
        print(" it's 'loaded'");

        printf("%s %s %f %f %f %d", owner, Intname, floatstr(X), floatstr(Y), floatstr(Z), strval(SQL));
        }
    }
    return 1;
}
rows gives you the number of rows back. So you can loop through them with a for loop, or a while, but you have to keep track of which rows you have done already.
Thanks, it's exactly what I did basically but only with a while loop +rap'ed
Quote:
Originally Posted by Mr_DjolE
Посмотреть сообщение
Too late.

--
You should of kept your code, for your efforts I would have given you 2 rep.. I'll give you the rep anyways :P
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)