Looking for an efficient way to verify the existance of an account
#1

Hi all,

I am working on a command that allows admins to offline ban an account. In order to do that I must verify whether or not the account actually exists in the database. Currently my disfunctional code is this:

This if-statement is in the offline ban command:

PHP код:
if(!DoesAccountExist(player))
        {
            
SendClientMessage(playeridCOLOR_RED"This account does not exist.");
        } 
And this is the code of DoesAccountExist:
PHP код:
DoesAccountExist(name[])
{
    new 
query[75];
    
format(querysizeof(query), "SELECT * FROM player WHERE name = '%s' LIMIT 1"EscapeString(name));
    return 
mysql_tquery(sql_connectionquery"OnDoesAccountExist");
}
forward OnDoesAccountExist();
public 
OnDoesAccountExist()
{
    new 
row_count;
    
cache_get_row_count(row_count);
    if(!
row_count) return 0;
    else return 
1;

This obviously does not work because mysql_tquery returns 1 or 0 depending on whether or not the query was executed succesfully, rather than what information it retrieved from the query.

What's the most efficient way to get what I am trying to get? Which is returning a 1 or 0 depending on if a Select statement returned any rows.
Reply
#2

You can send update query and check the affected rows count

PHP код:
/*Inside your ban command */
new query[75]; 
mysql_format(sql_connectionsizeof(query), "UPDATE player SET BANNED = 1 WHERE name = '%e'"name); //change the query according to your table structure 
mysql_tquery(sql_connectionquery"OnBanPlayer","d"playerid); //Playerid of player who executed command is passed to callback so as to send client message 
Then in the call back
PHP код:
forward OnBanPlayer(whomexecuted); 
public 
OnBanPlayer(whomexecuted

    
    if(!
cache_affected_rows()) return SendClientMessage(whomexecutedCOLOR_RED"This account does not exist."); 
    else
    { 
        
/*Ban the player...*/
        
SendClientMessage(whomexecutedCOLOR_RED"Banned player successfully.."); 
        return 
1;
    } 

Reply
#3

You can try this SQL statement, which is proven to be faster than COUNT(*).

PHP код:
SELECT EXISTS(SELECT 1 FROM player WHERE  name '%e' LIMIT 1
Also, you don't need "EscapeString", mysql_format has a custom specifier for escaping strings with '%e'.
Reply
#4

A non threaded query for what you're trying to achieve. SyS's method may be better though.
Reply
#5

Thank you! I am going to handle it that way.

Is there any way to get data out of the row that was affected by the UPDATE statement? In this particular case I am trying to do the following:

- Admin uses command to offline ban a player
- Update statement to be executed, if succesful the player is banned
- IP address and primary key of the affected player should be retrieved
- The IP address and primary key is to be used in 2 additional INSERT statements


Can I fetch the data of the affected row or should I execute a new SELECT statement?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)