Giving money with mysql query has no effect
#1

Hello everyone,

I've been out of scripting sa-mp for years now. I would like to get back into it a littlebit to play around with MySQL.
Now I am trying to give money to a player with the following function:

Код:
forward updateStats(playerid);
public updateStats(playerid)
{
    new query[128], playername[MAX_PLAYER_NAME];
    GetPlayerName(playerid, playername, sizeof(playername));
    mysql_format(mysql, query, sizeof(query), "SELECT `Money` FROM `accounts` WHERE `Name` = '%e' LIMIT 1", playername);
    mysql_tquery(mysql, query, "", "i", playerid);
    Player[playerid][Money] = cache_get_field_content_int(0, "Money");
	GivePlayerMoney(playerid, Player[playerid][Money]);
	SendClientMessage(playerid, -1, "Updated");
    return 1;
}
Now it does send the client message, but it doesn't give the player his money. Is there something wrong with the query or perhaps the mysql_tquery?

Thanks in advance
Reply
#2

Код:
forward updateStats(playerid);
public updateStats(playerid)
{
    new query[128], playername[MAX_PLAYER_NAME];
    GetPlayerName(playerid, playername, sizeof(playername));
    mysql_format(mysql, query, sizeof(query), "SELECT `Money` FROM `accounts` WHERE `Name` = '%s' LIMIT 1", playername);
    mysql_query(mysql, query);
    if(cache_num_rows())
   {
        Player[playerid][Money] = cache_get_field_content_int(0, "Money");
	GivePlayerMoney(playerid, Player[playerid][Money]);
	SendClientMessage(playerid, -1, "Updated");
    }
    return 1;
}
Reply
#3

Quote:
Originally Posted by F1N4L
Посмотреть сообщение
Код:
forward updateStats(playerid);
public updateStats(playerid)
{
    new query[128], playername[MAX_PLAYER_NAME];
    GetPlayerName(playerid, playername, sizeof(playername));
    mysql_format(mysql, query, sizeof(query), "SELECT `Money` FROM `accounts` WHERE `Name` = '%s' LIMIT 1", playername);
    mysql_query(mysql, query);
    if(cache_num_rows())
   {
        Player[playerid][Money] = cache_get_field_content_int(0, "Money");
	GivePlayerMoney(playerid, Player[playerid][Money]);
	SendClientMessage(playerid, -1, "Updated");
    }
    return 1;
}
Aah, great!

This also makes much more sense to me.

Thanks!
Reply
#4

mysql_tquery needs a callback to call the cache functions there otherwise using it directly will results in warnings for no active cache.

I can't understand the purpose of your code though. Why don't you simply give the money once when the login?

@F1N4L: You should store the cache result id and at the end to delete it to avoid memory leaks. Non-threaded queries are not very recommended either.
Reply
#5

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
@F1N4L: You should store the cache result id and at the end to delete it to avoid memory leaks. Non-threaded queries are not very recommended either.
Could you give me an example? Are you suggesting a loop?
I use this way, I have no problems, but if there is a safer way to get the values in the DB ...
Reply
#6

Quote:
Originally Posted by F1N4L
Посмотреть сообщение
Could you give me an example? Are you suggesting a loop?
I use this way, I have no problems, but if there is a safer way to get the values in the DB ...
Non-threaded queries: https://sampwiki.blast.hk/wiki/MySQL/R33#mysql_query
Threaded queries: https://sampwiki.blast.hk/wiki/MySQL/R33#mysql_tquery
Reply
#7

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
mysql_tquery needs a callback to call the cache functions there otherwise using it directly will results in warnings for no active cache.

I can't understand the purpose of your code though. Why don't you simply give the money once when the login?

@F1N4L: You should store the cache result id and at the end to delete it to avoid memory leaks. Non-threaded queries are not very recommended either.
Well, I want a live update on my server. So if the money gets updated from an external way (from a website by example) it should automatically change ingame as well. Don't want the player to have to logout first.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)