[Help - MySQL] Offline Ban not working
#1

I have this

PHP код:
CMD:oban(playeridparams[])// Banning offline players
{
    new 
bannedAdminName[MAX_PLAYER_NAME], PlayerName[MAX_PLAYER_NAME], str[128], reason[32], query[128], rows;
    
GetPlayerRPName(playeridAdminNamesizeof(AdminName));
    
GetPlayerName(bannedPlayerNamesizeof(PlayerName));
    if(
PlayerInfo[playerid][Admin] > 3)
    {
        if(
sscanf(params"s[24]"PlayerNamereason)) return SendClientMessage(playeridCOLOR_DARKCORAL"USAGE: /o(ffline)ban [Player NAME] [Reason]");
        
cache_get_row_count(rows);
        if(
rows 0)
        {
            
mysql_format(g_Sqlquerysizeof(query), "UPDATE `accounts` SET `isBanned` = 1 WHERE `Name` = '%e'"PlayerName);
            
mysql_tquery(g_Sqlquery"""");
            
format(strsizeof(str), "%s has offline banned %s. Reason: %s"AdminNamePlayerNamereason);
            
SendClientMessageToAll(COLOR_DARKCORALstr);       
        }
        else
        {
            
SendClientMessage(playeridCOLOR_DARKCORAL"That player doesn't exist!");
        }
    }
    else
    {
        
SendClientMessage(playeridCOLOR_DARKCORAL"You cannot use this command!");
    }
    return 
1;

When I use this in-game I get: That player doesn't exist!, even though they are in my database



SOLVED BY X337
Reply
#2

WHERE name = %s. U were using %e
Reply
#3

There's no active cache when you check for the row count. You have to either use an non-threaded query (not recommended!) or use mysql_tquery - like you are already - and check if the row count is > 0 in the callback that you called with mysql_tquery. There not being an active cache leaves 'rows' with the value of 0, which in return makes the if-statement false.

By the way, your mysql logs should tell you what the issue is.

Quote:
Originally Posted by verlaj
Посмотреть сообщение
WHERE name = %s. U were using %e
Same thing. Difference is that %e escapes the input and %s doesn't.
Reply
#4

Honestly, just check the output of the query. If affected rows is 1 then congrats you typed the right name and if its's 0 you didn't.
Reply
#5

Okay, so I made this

PHP код:
CMD:oban(playeridparams[])// Banning offline players
{
    new 
bannedAdminName[MAX_PLAYER_NAME], PlayerName[MAX_PLAYER_NAME], str[128], reason[32], query[128];
    
GetPlayerRPName(playeridAdminNamesizeof(AdminName));
    
GetPlayerName(bannedPlayerNamesizeof(PlayerName));
    if(
PlayerInfo[playerid][Admin] > 3)
    {
        if(
sscanf(params"s[24] s[24]"PlayerNamereason)) return SendClientMessage(playeridCOLOR_DARKCORAL"[BS] {AFAFAF}USAGE: /o(ffline)ban [Player NAME] [Reason]");
        
mysql_format(g_Sqlquerysizeof(query), "SELECT * FROM `accounts` WHERE `Name` = '%e' LIMIT 1"PlayerName);
        
mysql_tquery(g_Sqlquery"OffBan""is"playeridPlayerName);
        
printf(query);
        
format(strsizeof(str), "[BS] {AA3333} %s has offline banned %s. Reason: %s"AdminNamePlayerNamereason);
        
SendClientMessageToAll(COLOR_DARKCORALstr);
    }
    else
    {
        
SendClientMessage(playeridCOLOR_DARKCORAL"[BS] {AFAFAF}You cannot use this command!");
    }
    return 
1;

PHP код:
forward OffBan(playeridBanned); 
PHP код:
public OffBan(playeridBanned)
{
    new 
rowsfieldsquery[128], isOBanned 0;
    
cache_get_row_count(rows);
    
cache_get_field_count(fields);
    if(
rows)
    {
        
cache_get_value_name_int(0"isBanned"isOBanned);
        if(
isOBanned == 1)
        {
            
SendClientMessage(playeridCOLOR_DARKCORAL"[BS] {AFAFAF}That player is already banned!");
            return 
1;
        }
        
mysql_format(g_Sqlquerysizeof(query), "UPDATE `accounts` SET `isBanned` = 1 WHERE `Name` = %e"Banned);
        
mysql_tquery(g_Sqlquery"""");
        
printf(query);
    }
    else if(!
rows)
    {
        
SendClientMessage(playeridCOLOR_DARKCORAL"[BS] {AFAFAF}That player doesn't exist!");
        return 
1;
    }
    return 
1;

If I change "UPDATE `accounts` SET `isBanned` = 1 WHERE `Name` = %e" to "UPDATE `accounts` SET `isBanned` = 1", all the accounts get banned, so I don't know why I have a SELECT in the cmd: oban

Here's the output:





Also, how can I make a check if the return is 0 from the callback so I could do something? Like: If(isReturned(0)) return 1;
Reply
#6

You have to enclose a string with single or double quote in sql query.
Код:
WHERE `Name` = '%e'
And also, you can just execute update query without need to check if player name is exists in database. You can use cache_affected_rows() in the "OffBan" callback to check if player is already banned or doesn't exists.
Here's an example:
Код:
// In your oban cmd
mysql_format(g_Sql, query, sizeof(query), "UPDATE `accounts` SET `isBanned` = 1 WHERE `Name` =  '%e'", PlayerName); 
mysql_tquery(g_Sql, query, "OffBan", "is", playerid, PlayerName); 

public OffBan(playerid, Banned[]) 
{ 
	if(cache_affected_rows()) // It returns total of affected rows
	{
		new fmt[128];
		format(fmt, 128, "[BS] {AFAFAF}You have offline banned %s", Banned);
		SendClientMessage(playerid, COLOR_DARKCORAL, "[BS] {AFAFAF}Offline ban executed!");
		return 1;
	} 
	else
	{ 
		SendClientMessage(playerid, COLOR_DARKCORAL, "[BS] {AFAFAF}That player doesn't exist or already banned!"); 
		return 1; 
	} 
	return 1; 
}
Reply
#7

But, it still won't read the player's name, even with the single quotes, that was the problem. Also, thanks for the cache_affected_rows() tip, I will definitely use it If I could fix that problem
Reply
#8

Second parameter of "OffBan" callback is integer, it should be an array/string.
Код:
public OffBan(playerid, Banned[])
Reply
#9

Quote:
Originally Posted by X337
Посмотреть сообщение
Second parameter of "OffBan" callback is integer, it should be an array/string.
Код:
public OffBan(playerid, Banned[])
Thank you, you are awesome and I'm dumb. Have a rep
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)