Mysql update question, important.
#1

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.
Reply
#2

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.
Reply
#3

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.
Reply
#4

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.
Reply
#5

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.
Reply
#6

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"
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)