Mysql tquery loop only loading once
#1

So I have this loop inside my query function, after loading the first set of data, it stops. Instead of looping through until it reaches the end of szRows.

pawn Код:
stock LoadFactions()
{
    format(szQuery, sizeof(szQuery), "SELECT * FROM `factions`");
    mysql_tquery(Mysql_factions, szQuery, "Run_query", "d", RESULT_FACTIONS);

    return 1;
}

forward Run_query(resultid, extraid, ConnectionHandle);
public Run_query(resultid, extraid, ConnectionHandle)
{
    new szRows, szFields;

    if(resultid != 0)
    {
        cache_get_data(szRows, szFields);
    }
   
    switch(resultid)
    {
        case RESULT_FACTIONS:
        {
            if(szRows)
            {
                for(new i = 0; i < sizeof(szRows); i++)
                {
                    new factionID;
                    factionID = cache_get_field_content_int(i, "FactionID", Mysql_factions);
                    printf("%d", szRows);
                    FactionData[factionID][Created] = cache_get_field_content_int(0, "Created", Mysql_factions);
                    cache_get_field_content(0, "FactionName", FactionData[factionID][FactionName], Mysql_factions, 126);
                    FactionData[factionID][FactionType] = cache_get_field_content_int(0, "FactionType", Mysql_factions);
                    FactionData[factionID][hqX] = cache_get_field_content_float(0, "hqX", Mysql_factions);
                    FactionData[factionID][hqY] = cache_get_field_content_float(0, "hqY", Mysql_factions);
                    FactionData[factionID][hqZ] = cache_get_field_content_float(0, "hqZ", Mysql_factions);
                    FactionData[factionID][hqInterior] = cache_get_field_content_int(0, "hqInterior", Mysql_factions);
                    FactionData[factionID][hqWorld] = i;
                    FactionData[factionID][lockerX] = cache_get_field_content_float(0, "lockerX", Mysql_factions);
                    FactionData[factionID][lockerY] = cache_get_field_content_float(0, "lockerY", Mysql_factions);
                    FactionData[factionID][lockerZ] = cache_get_field_content_float(0, "lockerZ", Mysql_factions);
                    FactionData[factionID][lockerInterior] = cache_get_field_content_int(0, "lockerInterior", Mysql_factions);
                    FactionData[factionID][lockerWorld] = i;
                    cache_get_field_content(0, "Rank1", FactionData[factionID][Rank1], Mysql_factions, 126);
                    cache_get_field_content(0, "Rank2", FactionData[factionID][Rank2], Mysql_factions, 126);
                    cache_get_field_content(0, "Rank3", FactionData[factionID][Rank3], Mysql_factions, 126);
                    cache_get_field_content(0, "Rank4", FactionData[factionID][Rank4], Mysql_factions, 126);
                    cache_get_field_content(0, "Rank5", FactionData[factionID][Rank5], Mysql_factions, 126);
                    cache_get_field_content(0, "Rank6", FactionData[factionID][Rank6], Mysql_factions, 126);
                    cache_get_field_content(0, "Division1", FactionData[factionID][Division1], Mysql_factions, 126);
                    cache_get_field_content(0, "Division2", FactionData[factionID][Division2], Mysql_factions, 126);
                    cache_get_field_content(0, "Division3", FactionData[factionID][Division3], Mysql_factions, 126);
                    cache_get_field_content(0, "Division4", FactionData[factionID][Division4], Mysql_factions, 126);
                    cache_get_field_content(0, "Division5", FactionData[factionID][Division5], Mysql_factions, 126);
                    cache_get_field_content(0, "Division6", FactionData[factionID][Division6], Mysql_factions, 126);
                   
                    printf("Loaded faction: %s (%d)", FactionData[factionID][FactionName], factionID);
                }
            }
         }
    }
    return 1;
}
The print is printing 3 szRows, but it only loads row 0 and then stops completely, instead of loading the next two.
Reply
#2

Please DON'T use Hungarian notation if you have no idea HOW to use it. "sz" means "string, zero-terminated". It's not a prefix for anything!

Yet, I don't see anything inherently wrong with your code. Does it reach here?
Quote:
pawn Код:
printf("Loaded faction: %s (%d)", FactionData[factionID][FactionName], factionID);
Reply
#3

Yes Vince, it reaches there. It prints that and then stops instead of looping through the rest of the rows.

Also, are you saying dont use "szRows"? Ive seen so many mysql scripts use them!
Reply
#4

You are using "sizeof(szRows)" within the loop. The loop only runs once because using "sizeof" with a non-array variable returns 1, so try changing:

pawn Код:
for(new i = 0; i < sizeof(szRows); i++)
To:

pawn Код:
for (new i = 0; i < szRows; i ++)
Reply
#5

Quote:
Originally Posted by Emmet_
Посмотреть сообщение
You are using "sizeof(szRows)" within the loop. The loop only runs once because using "sizeof" with a non-array variable returns 1, so try changing:

pawn Код:
for(new i = 0; i < sizeof(szRows); i++)
To:

pawn Код:
for (new i = 0; i < szRows; i ++)
Ahh finally, wonderful! Thank you so much Emmet.

Edit:
Noticed when it done the "Loaded faction:" print that it loaded the same faction for X amount of rows, instead of the next faction. Although it loaded the next ID.

Example, I have in my database:
Код:
factionid factionname
12 Corrupt
11 Fire Department
10 Health Department
Print:
Код:
Loaded faction: Corrupt (12)
Loaded faction: Corrupt (11)
Loaded faction: Corrupt (10)
Edit again:
Nevermind, made a blonde mistake, noticed I was loading row 0 instead of 'i' for most of the information. *facepalm*
Reply
#6

Quote:
Originally Posted by Jack_Leslie
Посмотреть сообщение
Also, are you saying dont use "szRows"? Ive seen so many mysql scripts use them!
Then those "many mysql scripts" (link?) are also doing it wrong. There seems to be some misinformation going around the community regarding "sz". I think some just use it to seem professional or profound, without having the slightest idea what it means (no offense to you intended).
Reply
#7

Quote:
Originally Posted by Vince
Посмотреть сообщение
Then those "many mysql scripts" (link?) are also doing it wrong. There seems to be some misinformation going around the community regarding "sz". I think some just use it to seem professional or profound, without having the slightest idea what it means (no offense to you intended).
No offense taken - I've just gone by what I've seen, but thank you for pointing that out for future reference, appreciate it!

Edit:
This one gamemode that I downloaded to look through and learn from examples (I always learn best by reading through scripts rather then someone explaining or reading tutorials) https://sampforum.blast.hk/showthread.php?tid=513424
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)