MySQL problem
#1

Hi, I've a problem with my inventory saving system.

pawn Код:
stock SavePlayerInventory(playerid) {
    new id = PlayerInfo[playerid][pID];
    new query[256], temp;
    for(new i = 0; i < MAX_ITEM; ++i)
    {
        if(PlayerInv[playerid][i] > 0) {
            mysql_format(DB,query,sizeof(query), "SELECT * FROM `playerinventory` WHERE pid = '%d' AND item = '%d'", id, i);
            mysql_tquery(DB,query);
            printf("%s", query);
            new nbRow = cache_num_rows();
            if(nbRow > 0) {
                temp = cache_get_field_content_int(0, "amount");
                if(temp != PlayerInv[playerid][i]) {
                    mysql_format(DB,query,sizeof(query), "UPDATE FROM `playerinventory` SET amount = '%d' WHERE pid = '%d' AND item = '%d'",PlayerInv[playerid][i], id, i);
                    mysql_tquery(DB,query);
                    printf("%s", query);
                }
            } else {
                mysql_format(DB, query, sizeof(query), "INSERT INTO `playerinventory` (pid,item,amount) VALUES (\'%d\', \'%d\' , \'%d\')", id, i, PlayerInv[playerid][i]);
                mysql_tquery(DB, query);
                printf("%s", query);
            }
        }
    }
    return 1;
}
Even if the item already exist in the database, it goes for an INSERT query, so i've got 2 time the same item.

Thanks in advance.
Reply
#2

Well, your best off using mysql_num_rows to see if it exists, that's what most people use.
Reply
#3

I'm using MySQL R38 plugin, so "mysql_num_rows" function doesn't exist. I think a I've to save my query result in cache but I don't know how to do that.
Reply
#4

Quote:
Originally Posted by PakPak
Посмотреть сообщение
I'm using MySQL R38 plugin, so "mysql_num_rows" function doesn't exist. I think a I've to save my query result in cache but I don't know how to do that.
I strongly recommend you use BlueG's MySQL. Most people use this one and most tutorials are with it.
Reply
#5

Use cache_num_rows()
Reply
#6

Combine pid and item to make a unique key. Then use only a single query, rather than two:
PHP код:
INSERT INTO playerinventory (pid,item,amountVALUES (%d, %, %dON DUPLICATE KEY UPDATE amount = %
See also here for a similar structure: https://sampforum.blast.hk/showthread.php?tid=505081
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)