Loading clans from DB
#1

So, I started working on a new project with around 4 members..
I got struck up in loading the clans from the database [MySQL R-38]

pawn Код:
forward LoadClans();
public LoadClans()
{
    for(new c = 1; c <= MAX_CLANS; c++)
    {
        mysql_format(MySQL, gQuery, 256, "SELECT * FROM `"Clans_Table"` WHERE `cID` = '%i'", c);
        mysql_tquery(MySQL, gQuery, "", "");

        new rows, fields;
        cache_get_data(rows, fields, MySQL);
        if(!rows) break;
        else if(rows)
        {
            cInfo[c][cID] = cache_get_field_content_int(0, "cID");
            cache_get_field_content(0, "cName", cInfo[c][cName], MySQL, 50);
            cache_get_field_content(0, "cOwner", cInfo[c][cOwner], MySQL, MAX_PLAYER_NAME);
            cache_get_field_content(0, "cOwner2", cInfo[c][cOwner2], MySQL, MAX_PLAYER_NAME);
            cInfo[c][cMembers] = cache_get_field_content_int(0, "cMembers");
            cInfo[c][cScore] = cache_get_field_content_int(0, "cScore");
        }
        ClansCreated++;
    }
    printf("[Bronze Stunting] Loaded %i clans.", ClansCreated);
    return 1;
}
So, There's already a clan with ID 1 in the database, but it doesn't load it. ;o
Can someone help me in solving this?
Reply
#2

Don't use a global string for your queries. Try simply "if (rows)" to check if a row was returned.
Reply
#3

Alright, Now I used it like this:
pawn Код:
forward LoadClans();
public LoadClans()
{
    new TempQuery[1028];
    for(new c = 1; c <= MAX_CLANS; c++)
    {
        mysql_format(MySQL, TempQuery, sizeof(TempQuery), "SELECT * FROM `"Clans_Table"` WHERE `cID` = '%i'", c);
        mysql_tquery(MySQL, TempQuery, "", "");

        new rows, fields;
        cache_get_data(rows, fields, MySQL);
        if(!rows) break;
        else if(rows)
        {
            cInfo[c][cID] = cache_get_field_content_int(0, "cID");
            cache_get_field_content(0, "cName", cInfo[c][cName], MySQL, 50);
            cache_get_field_content(0, "cOwner", cInfo[c][cOwner], MySQL, MAX_PLAYER_NAME);
            cache_get_field_content(0, "cOwner2", cInfo[c][cOwner2], MySQL, MAX_PLAYER_NAME);
            cInfo[c][cMembers] = cache_get_field_content_int(0, "cMembers");
            cInfo[c][cScore] = cache_get_field_content_int(0, "cScore");
        }
        ClansCreated++;
    }
    printf("[Bronze Stunting] Loaded %i clans.", ClansCreated);
    return 1;
}
And I had 1 clan in the database (testing)
It didn't load.

mysql_log
Код:
[21:22:31] [DEBUG] CMySQLConnection::Connect - establishing connection to database...
[21:22:31] [DEBUG] CMySQLConnection::Connect - connection was successful
[21:22:31] [DEBUG] CMySQLConnection::Connect - auto-reconnect has been enabled
[21:22:31] [DEBUG] CMySQLConnection::Connect - connection was successful
[21:22:31] [DEBUG] CMySQLConnection::Connect - auto-reconnect has been enabled
[21:22:31] [DEBUG] CMySQLConnection::Connect - connection was successful
[21:22:31] [DEBUG] CMySQLConnection::Connect - auto-reconnect has been enabled
[21:22:31] [DEBUG] mysql_format - connection: 1, len: 1028, format: "SELECT * FROM `BSClans321` WHERE `cID` = '%i'"
[21:22:31] [DEBUG] mysql_tquery - connection: 1, query: "SELECT * FROM `BSClans321` WHERE `cID` = '1'", callback: "(null)", format: "(null)"
[21:22:31] [DEBUG] cache_get_data - connection: 1
[21:22:31] [WARNING] cache_get_data - no active cache
[21:22:31] [DEBUG] mysql_errno - connection: 1
[21:22:31] [DEBUG] mysql_errno - connection: 1
[21:22:32] [DEBUG] CMySQLQuery::Execute[] - starting query execution
[21:22:32] [DEBUG] CMySQLQuery::Execute[] - query was successfully executed within 1.331 milliseconds
[21:22:32] [DEBUG] CMySQLQuery::Execute[] - no callback specified, skipping result saving
Reply
#4

pawn Код:
forward LoadClans();
public LoadClans()
{
        mysql_format(MySQL, TempQuery, sizeof(TempQuery), "SELECT * FROM `"Clans_Table"` WHERE `cID` = '%i'", c);
Why have you quoted Clans_Table inside of the ``?

surely it would be like this:
pawn Код:
forward LoadClans();
public LoadClans()
{
        mysql_format(MySQL, TempQuery, sizeof(TempQuery), "SELECT * FROM `Clans_Table` WHERE `cID` = '%i'", c);
Reply
#5

No. Are you insane?
I have used
pawn Код:
#define Clans_Table     "tabname"
so when i'm using it somewhere i need to use "Clans_Table"
Reply
#6

Try what I said, I'm interested.
Reply
#7

That's not how threaded queries work. It needs a callback to load the data there and not directly (non-threaded or using y_inline).

Last, you'll need to loop N (N = rows) times and not as many slots as the cInfo has.
Reply
#8

If you can kindly provide me an example, that'd be great!
Reply
#9

pawn Код:
LoadClans()
{
    mysql_format(MySQL, gQuery, 256, "SELECT * FROM "Clans_Table"");
    mysql_tquery(MySQL, gQuery, "CB_LoadClans", "");
}

forward CB_LoadClans();
public CB_LoadClans()
{
    for (new i = 0, rows = cache_get_row_count(); i != rows; ++i)
    {
        if (i >= sizeof (cInfo)) break;
        cInfo[i][cID] = cache_get_field_content_int(i, "cID");
        cache_get_field_content(i, "cName", cInfo[i][cName], MySQL, 50);
        cache_get_field_content(i, "cOwner", cInfo[i][cOwner], MySQL, MAX_PLAYER_NAME);
        cache_get_field_content(i, "cOwner2", cInfo[i][cOwner2], MySQL, MAX_PLAYER_NAME);
        cInfo[i][cMembers] = cache_get_field_content_int(i, "cMembers");
        cInfo[i][cScore] = cache_get_field_content_int(i, "cScore");
        ClansCreated++;
    }
    printf("[Bronze Stunting] Loaded %i clans.", ClansCreated);
}
Reply
#10

Konstantinos strikes back! Use what he posted, though I still urge you to delete that global string "gQuery" and use local strings instead!
Plus, Kontantinos, there's no need AFAIK, to call mysql_format, it's a simple query and there are no placeholders, you can simply directly input that to "mysql_tquery".
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)