[MySQL] /f(action) bug | Reading a string from the SQL database
#1

Okay, here's a brief summary, my faction system is functional to an extent but is very flimsy. I want a dynamic changeable rank and faction name system that is changeable via the database and via command. Here's my code.

pawn Код:
// Here is the faction info Enum

enum factionstats
{
    factionid,
    factionname[32],
    factiontype,
    rank10name[32],
    rank9name[32],
    rank8name[32],
    rank7name[32],
    rank6name[32],
    rank5name[32],
    rank4name[32],
    rank3name[32],
    rank2name[32],
    rank1name[32],
    factionbank, // Add more info
}
new FVar[MAX_FACTIONS][factionstats];

// -------------------------------
// Here is the on game mode faction loading

public OnGameModeInit()
{
    SetGameModeText("SF-RP v0.1b");
    AddPlayerClass(1,-2289.6543,211.1139,35.3125, 0,0,0,0,0,0,0);
    new Hour, Minute, Second;
    gettime(Hour, Minute, Second);
    SetWorldTime(Hour);

    mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASS);
    mysql_debug(1);
    loadarrowpickups();
    loadmaps();
    loadfactions();
   
    DisableInteriorEnterExits();
    SetTimer("MoneyTimer", 1000, 1);
    return 1;
}

// ----------------------------
// Loading the factions from the DB

stock LoadFactionInfo(iFaction)
{
    new Query[700];
    if(mysql_fetch_row(Query))
    {
        sscanf(Query, "p<|>e<ds[32]ds[32]s[32]s[32]s[32]s[32]s[32]s[32]s[32]s[32]s[32]d>", FVar[iFaction]);
        mysql_free_result();
    }
    return 1;
}

stock loadfactions()
{
    new Query[90], factions;
    format(Query, sizeof(Query), "SELECT * FROM `factioninfo`");
    mysql_query(Query);
    mysql_store_result();
    factions = mysql_num_rows();
   
    for(new i = 0; i != factions; i++)
    {
        LoadFactionInfo(i);
    }
    print("Factions loaded");
    return 1;
}

//-------------------------------
// Displaying the factions code (Debugging command)

// !THIS WORKS!
command(factions, playerid, params[])
{
    new str[128];
    for(new i = 0; i != MAX_FACTIONS; i++)
    {
        if(FVar[i][factionid] != 0)
        {
            format(str, sizeof(str), "%s, (ID %d)", FVar[i][factionname], FVar[i][factionid]);
            SendClientMessage(playerid, 0xFFFFFFFF, str);
        }
    }
    return 1;
}

// -------------------
// Faction Chat

command(f, playerid, params[])
{
    new str[128], message[128];
    if(!sscanf(params, "s[128]", message))
    {
        if(PVar[playerid][factionID] < 0)
        {
            SendClientMessage(playerid, 0x66666666, "You are not currently in a faction");
            return 1;
        }
        else
        {
            for(new i = 0; i != MAX_PLAYERS; i++)
            {
                if(PVar[i][factionID] == PVar[playerid][factionID])
                {
                    new faction = PVar[playerid][factionID];
                    new rank = PVar[playerid][factionRank];
                    new rankdisp[32];

                    rankdisp = FactionRankToName(faction, rank);
                    format(str, sizeof(str), "[%s, %s]: %s", rankdisp, RemoveUnderScore(playerid), message);
                    SendClientMessage(i, 0xFFFFFFFF, str);
                }
            }
            return 1;
        }
    }
    else
    {
        SendClientMessage(playerid, 0x66666666, "Usage: /f(action) [Message]");
        return 1;
    }
}
That's about it for code, here's the results.



I'm not sure what the issue is, maybe the conflicting faction IDs.

Here's a SS of how my database works:


Reply
#2

try to make some debug's to see in which part fall's your code.
Reply
#3

Quote:
Originally Posted by leonardo1434
Посмотреть сообщение
try to make some debug's to see in which part fall's your code.
Can you please elaborate a little? I can't see where there would be a suitable place to debug.
Reply
#4

debug the strings that are being parsed by each function and see where it stop to work.
Reply
#5

Quote:
Originally Posted by leonardo1434
Посмотреть сообщение
debug the strings that are being parsed by each function and see where it stop to work.
I printed the rank name before returning it back to the command and in the console it says:

<null>
Reply
#6

Maybe you can try this:

pawn Код:
stock LoadFactionInfo(iFaction)
{
    new Query[700];
    format(Query, sizeof(Query), "SELECT * FROM `factioninfo` WHERE `factionid` = '%d'", iFaction);
    mysql_query(Query);
    mysql_store_result();
    if (mysql_num_rows())
    {
        mysql_fetch_row_format(Query, "|");
        sscanf(Query, "e<p<|><ds[32]ds[32]s[32]s[32]s[32]s[32]s[32]s[32]s[32]s[32]s[32]d>", FVar[iFaction]);
    }    
    mysql_free_result();
    return 1;
}

stock loadfactions()
{
    for(new i = 0; i != MAX_FACTIONS; i++)
    {
        LoadFactionInfo(i);
    }
    print("Factions loaded");
    return 1;
}
You stored a result one time and free'd a result like 10 times.
Reply
#7

Quote:
Originally Posted by Emmet_
Посмотреть сообщение
Maybe you can try this:

pawn Код:
stock LoadFactionInfo(iFaction)
{
    new Query[700];
    format(Query, sizeof(Query), "SELECT * FROM `factioninfo` WHERE `factionid` = '%d'", iFaction);
    mysql_query(Query);
    mysql_store_result();
    if (mysql_num_rows())
    {
        mysql_fetch_row_format(Query, "|");
        sscanf(Query, "e<p<|><ds[32]ds[32]s[32]s[32]s[32]s[32]s[32]s[32]s[32]s[32]s[32]d>", FVar[iFaction]);
    }    
    mysql_free_result();
    return 1;
}

stock loadfactions()
{
    for(new i = 0; i != MAX_FACTIONS; i++)
    {
        LoadFactionInfo(i);
    }
    print("Factions loaded");
    return 1;
}
You stored a result one time and free'd a result like 10 times.
Thanks, that's much more efficient but, I'm afraid it's still not returning the string.. I'm continuing to work on it but having the same result.
Reply
#8

can you post your enum?
Reply
#9

Quote:
Originally Posted by leonardo1434
Посмотреть сообщение
can you post your enum?
Sure:

pawn Код:
enum factionstats
{
    factionid,
    factionname[32],
    factiontype,
    rank10name[32],
    rank9name[32],
    rank8name[32],
    rank7name[32],
    rank6name[32],
    rank5name[32],
    rank4name[32],
    rank3name[32],
    rank2name[32],
    rank1name[32],
    factionbank, // Add more info
}
new FVar[MAX_FACTIONS][factionstats];
Reply
#10

i'm not too good in sscanf in a advanced way, but it should be like this i guess. it's working fine for me. here's what i made, just a little test.
pawn Код:
enum factionstats
{
    factionid,
    factionname[32],
    factiontype,
    rank10name[32],
    rank9name[32],
    rank8name[32],
    rank7name[32],
    rank6name[32],
    rank5name[32],
    rank4name[32],
    rank3name[32],
    rank2name[32],
    rank1name[32],
    factionbank, // Add more inf
}
new FVar[MAX_FACTIONS][factionstats];
public OnFilterScriptInit()
{
   new Query[700] = "1|i|2|m|testing|this|now|,|looks|cool|but|fuck|yeh|3|";
   sscanf(Query, "p<|>e<is[32]is[32]s[32]s[32]s[32]s[32]s[32]s[32]s[32]s[32]s[32]i>", FVar[0]);
   printf(#%d - %s - %d - %s - %s -%s -%s -%s -%s -%s -%s -%s -%s - %d,
   FVar[0][factionid],
   FVar[0][factionname],
   FVar[0][factiontype],
   FVar[0][rank10name],
   FVar[0][rank9name],
   FVar[0][rank8name],
   FVar[0][rank7name],
   FVar[0][rank6name],
   FVar[0][rank5name],
   FVar[0][rank4name],
   FVar[0][rank3name],
   FVar[0][rank2name],
   FVar[0][rank1name],
   FVar[0][factionbank]);
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)