SA-MP Forums Archive
Mysql update question, important. - 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 update question, important. (/showthread.php?tid=424864)



Mysql update question, important. - PaulDinam - 24.03.2013

How can I update in the sql like something.
like I want to edit player stats when he's offline, to add cash not set.

pawn Код:
format(query, sizeof(query), "SELECT `cash` FROM `users` WHERE `name` = '%s'", name);
mysql_function_query(dbHandle, query, true, "AddPlayerMoney", "ii", name, cash);

The cache:
forward AddPlayerMoney(name[], cash);
public AddPlayerMoney(name[], cash)
{
    new rows, fields, cash2, query[256];
    cache_get_data(rows, fields);
    cash2 = cache_get_row_int(0, 5);
    cash += cash2;
    format(query, sizeof(query), "UPDATE `users` SET `cash` = %d WHERE `name` = '%s'", cash, name);
    mysql_function_query(dbHandle, query, true, "", "");
}
it doesn't works.


Re: Mysql update question, important. - ReneG - 24.03.2013

You don't need to fetch any data, just update.
pawn Код:
forward AddPlayerMoney(name[], cash);
public AddPlayerMoney(name[], cash)
{
    new query[128];
    format(query, sizeof(query), "UPDATE `users` SET `cash` = %d WHERE `name` = '%s'", cash, name);
    mysql_function_query(dbHandle, query, false, "", "");
    return 1;
}
Then just pass name and cash values as arguments to AddPlayerMoney from a command or something.


Re: Mysql update question, important. - Maxips2 - 24.03.2013

When using cache_get functions, the index you specify is relative to your query, not to the order of your table structure.

Also, you might want to check if there are any rows before doing anything with the result.


Re: Mysql update question, important. - PaulDinam - 24.03.2013

I have fixed it thanks though, all I did is, SELECT * FROM `users` where is the name.
then if rows, i'm just caching the money to variable and doing another query,
to set the money again for the player.


Re: Mysql update question, important. - Maxips2 - 24.03.2013

Quote:
Originally Posted by PaulDinam
Посмотреть сообщение
I have fixed it thanks though, all I did is, SELECT * FROM `users` where is the name.
then if rows, i'm just caching the money to variable and doing another query,
to set the money again for the player.
The reason it works this way is because you select all fields in your table instead of what you need.


Re: Mysql update question, important. - ReneG - 24.03.2013

Maxips2 is right.

This would be more efficient than using the * wildcard
pawn Код:
format(query, sizeof(query), "SELECT `cash` FROM `users` WHERE `name` = '%s'", name);
mysql_function_query(dbHandle, query, true, "AddPlayerMoney", "si", name, cash);

The cache:
forward AddPlayerMoney(name[], cash);
public AddPlayerMoney(name[], cash)
{
    new rows, fields, query[128];
    cache_get_data(rows, fields);

    if(!rows) return 0;

    format(query, sizeof(query), "UPDATE `users` SET `cash` = %d WHERE `name` = '%s'", cash + cache_get_row_int(0, 0), name);
    mysql_function_query(dbHandle, query, true, "", "");
}
Also, in your first mysql_function_query, you had two i's, it should have been "si"