MySQL problem - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: MySQL problem (
/showthread.php?tid=516486)
MySQL problem -
PakPak - 31.05.2014
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.
Re: MySQL problem -
nmader - 31.05.2014
Well, your best off using mysql_num_rows to see if it exists, that's what most people use.
Re: MySQL problem -
PakPak - 31.05.2014
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.
Re: MySQL problem -
nmader - 01.06.2014
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.
Re: MySQL problem -
DobbysGamertag - 01.06.2014
Use cache_num_rows()
Re: MySQL problem -
Vince - 01.06.2014
Combine
pid and
item to make a unique key. Then use only a single query, rather than two:
PHP код:
INSERT INTO playerinventory (pid,item,amount) VALUES (%d, %d , %d) ON DUPLICATE KEY UPDATE amount = %d
See also here for a similar structure:
https://sampforum.blast.hk/showthread.php?tid=505081