MYSQL loading problem
#1

Hi everyone,

I'm pretty new with loading massive MYSQL things, and am stuck on my Factions loading part...
here is the function:
pawn Код:
forward LoadFactions();
public LoadFactions()
{

    new Query[300];
    for(new i = 0; i < MAX_FACTIONS; i++)
    {
    format(Query, sizeof(Query), "SELECT * FROM `Factions` WHERE `FactionID` = '%s' ", FactionCount);
    mysql_query(Query);
    mysql_store_result();
    mysql_fetch_row_format(Query);
    sscanf(Query, "e<p<|>is[128]s[128]dfff>", FInfo[i]);
    mysql_free_result();
    FactionCount++;
    new string[128];
    format(string,sizeof(string),"Faction Name: %s \nOwned by: %s\nFaction Value: %d",FInfo[i][Name],FInfo[i][Owner],FInfo[i][Value]);
    CreateDynamicPickup(1239,1,FInfo[i][LocationX],FInfo[i][LocationY],FInfo[i][LocationZ],-1,-1,-1,100.0);
    CreateDynamic3DTextLabel(string,COLOR_RED,FInfo[i][LocationX],FInfo[i][LocationY],FInfo[i][LocationZ],100.0);
   
    }
}
I thought it was right, but apperently not..
Reply
#2

pawn Код:
for(new i = 0; i < MAX_FACTIONS; i++)
    {
    format(Query, sizeof(Query), "SELECT * FROM `Factions` WHERE `FactionID` = '%s' ", FactionCount);
Who teaches that kind of stuff? This completely ruins the point of MySQL. You're accessing each row as if it was a file. Learn the use of while loops.

pawn Код:
forward LoadFactions();
public LoadFactions()
{
    new
        Query[300],
        string[128],
        FactionCount;
   
    mysql_query("SELECT * FROM Factions");
    mysql_store_result();

    while(mysql_fetch_row_format(Query, "|"))
    {
        sscanf(Query, "e<p<|>is[128]s[128]dfff>", FInfo[i]);
        format(string,sizeof(string),"Faction Name: %s \nOwned by: %s\nFaction Value: %d", FInfo[i][Name], FInfo[i][Owner], FInfo[i][Value]);

        CreateDynamicPickup(1239, 1, FInfo[i][LocationX], FInfo[i][LocationY], FInfo[i][LocationZ], -1, -1, -1, 100.0);
        CreateDynamic3DTextLabel(string, COLOR_RED, FInfo[i][LocationX], FInfo[i][LocationY], FInfo[i][LocationZ], 100.0);

        FactionCount++;
    }
   
    mysql_free_result();
    return FactionCount;
}
Reply
#3

Quote:
Originally Posted by Vince
Посмотреть сообщение
pawn Код:
for(new i = 0; i < MAX_FACTIONS; i++)
    {
    format(Query, sizeof(Query), "SELECT * FROM `Factions` WHERE `FactionID` = '%s' ", FactionCount);
Who teaches that kind of stuff? This completely ruins the point of MySQL. You're accessing each row as if it was a file. Learn the use of while loops.

pawn Код:
forward LoadFactions();
public LoadFactions()
{
    new
        Query[300],
        string[128],
        FactionCount;
   
    mysql_query("SELECT * FROM Factions");
    mysql_store_result();

    while(mysql_fetch_row_format(Query, "|"))
    {
        sscanf(Query, "e<p<|>is[128]s[128]dfff>", FInfo[i]);
        format(string,sizeof(string),"Faction Name: %s \nOwned by: %s\nFaction Value: %d", FInfo[i][Name], FInfo[i][Owner], FInfo[i][Value]);

        CreateDynamicPickup(1239, 1, FInfo[i][LocationX], FInfo[i][LocationY], FInfo[i][LocationZ], -1, -1, -1, 100.0);
        CreateDynamic3DTextLabel(string, COLOR_RED, FInfo[i][LocationX], FInfo[i][LocationY], FInfo[i][LocationZ], 100.0);

        FactionCount++;
    }
   
    mysql_free_result();
    return FactionCount;
}
Thanks alot Vince
Nobody teached it to me, so i tried to do something myself, but now, I can do it alot better..
Thanks

EDIT: ow wait... What do I have to do instead of I?
I mean, i had Finfo[I], what do I have to do now? FInfo[]

And,

Is the saving part correct?
pawn Код:
forward SaveFactions();
public SaveFactions()
{
    for(new i = 0; i < FactionCount; i++)
    {
    new query[1024];
    format(query, sizeof(query), "UPDATE Factions SET \
    FactionID = '%i', \
    Name = '%s',\
    Owner = '%s',\
    Value = '%d',\
    LocationX = '%f',\
    LocationY = '%f',\
    LocationZ = '%f'\
    WHERE FactionID = '%i'"
,
    FInfo[i][FactionID],
    FInfo[i][Name],
    FInfo[i][Owner],
    FInfo[i][Value],
    FInfo[i][LocationX],
    FInfo[i][LocationY],
    FInfo[i][LocationZ],
    FInfo[i][FactionID]);
    mysql_query(query);
    }

   
}
Reply
#4

Set i to 0 before the loop and increment in the loop.
pawn Код:
new i = 0;
while(mysql_fetch_row_format(Query, "|"))
{
    // Put data into array (index i)
    i ++;
}
Based on Vince's code.

Hopefully you get the idea, and as he pointed out, MySQL is very powerful and allows you to select a lot of data at once.
Reply
#5

Quote:
Originally Posted by AndreT
Посмотреть сообщение
Set i to 0 before the loop and increment in the loop.
pawn Код:
new i = 0;
while(mysql_fetch_row_format(Query, "|"))
{
    // Put data into array (index i)
    i ++;
}
Based on Vince's code.

Hopefully you get the idea, and as he pointed out, MySQL is very powerful and allows you to select a lot of data at once.
I don't understand it anymore..
Do I have to use the loop or not?

Could someone post the whole code , so im sure i make no mistakes?

Thanks alot..
And, is my saving part ok? or is tht wrong too?
Reply
#6

BUMP - Sorry, but i still have no progress...
I don't know how to use the following code
Reply
#7

Seriously? Anyone?
Is there no one who can explain me how to use the function Vince gave me?
((Code from Vince))
pawn Код:
forward LoadFactions();
public LoadFactions()
{
    new
        Query[300],
        string[128],
        FactionCount;
   
    mysql_query("SELECT * FROM Factions");
    mysql_store_result();

    while(mysql_fetch_row_format(Query, "|"))
    {
        sscanf(Query, "e<p<|>is[128]s[128]dfff>", FInfo[i]);
        format(string,sizeof(string),"Faction Name: %s \nOwned by: %s\nFaction Value: %d", FInfo[i][Name], FInfo[i][Owner], FInfo[i][Value]);

        CreateDynamicPickup(1239, 1, FInfo[i][LocationX], FInfo[i][LocationY], FInfo[i][LocationZ], -1, -1, -1, 100.0);
        CreateDynamic3DTextLabel(string, COLOR_RED, FInfo[i][LocationX], FInfo[i][LocationY], FInfo[i][LocationZ], 100.0);

        FactionCount++;
    }
   
    mysql_free_result();
    return FactionCount;
}
It gives an undefined symbol I..
what should I do here?
Reply
#8

pawn Код:
forward LoadFactions();
public LoadFactions()
{
    new
        Query[300],
        string[128],
        FactionCount;

    mysql_query("SELECT * FROM Factions");
    mysql_store_result();

    new i = 0;
    while(mysql_fetch_row_format(Query, "|"))
    {
        sscanf(Query, "e<p<|>is[128]s[128]dfff>", FInfo[i]);
        format(string,sizeof(string),"Faction Name: %s \nOwned by: %s\nFaction Value: %d", FInfo[i][Name], FInfo[i][Owner], FInfo[i][Value]);

        CreateDynamicPickup(1239, 1, FInfo[i][LocationX], FInfo[i][LocationY], FInfo[i][LocationZ], -1, -1, -1, 100.0);
        CreateDynamic3DTextLabel(string, COLOR_RED, FInfo[i][LocationX], FInfo[i][LocationY], FInfo[i][LocationZ], 100.0);

        FactionCount++;
        i++;
    }

    mysql_free_result();
    return FactionCount;
}
pawn Код:
forward SaveFactions();
public SaveFactions()
{
    new query[1024];
    for(new i = 0; i < FactionCount; i++)
    {
        format(query, sizeof(query), "UPDATE Factions SET \
                Name = '%s',\
                Owner = '%s',\
                Value = '%d',\
                LocationX = '%f',\
                LocationY = '%f',\
                LocationZ = '%f'\
                WHERE FactionID = '%i'"
,
            FInfo[i][Name],
            FInfo[i][Owner],
            FInfo[i][Value],
            FInfo[i][LocationX],
            FInfo[i][LocationY],
            FInfo[i][LocationZ],
            FInfo[i][FactionID]);
        mysql_query(query);
    }
}
But saving in a loop like that is not recommended. You should save each faction separately when something is changed

pawn Код:
forward SaveFaction(i);
public SaveFaction(i)
{
    new query[1024];
    format(query, sizeof(query), "UPDATE Factions SET \
            Name = '%s',\
            Owner = '%s',\
            Value = '%d',\
            LocationX = '%f',\
            LocationY = '%f',\
            LocationZ = '%f'\
            WHERE FactionID = '%i'"
,
        FInfo[i][Name],
        FInfo[i][Owner],
        FInfo[i][Value],
        FInfo[i][LocationX],
        FInfo[i][LocationY],
        FInfo[i][LocationZ],
        FInfo[i][FactionID]);
    mysql_query(query);
}
Reply
#9

Thanks alot MadeMan, I will try this now
Reply
#10

Hmm.. so I tried Mademan's code, the saving part works, but the loading part refuses to work.
It just doesn't load, and doesn't create the pickup....
Just to make sure this is not the problem, I'm using BlueG's MYSQL plugin, the R6 version, because my server isn't working with R7
Anyone?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)