Looking for an efficient way to verify the existance of an account -
Robin96 - 11.06.2018
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(playerid, COLOR_RED, "This account does not exist.");
}
And this is the code of DoesAccountExist:
PHP код:
DoesAccountExist(name[])
{
new query[75];
format(query, sizeof(query), "SELECT * FROM player WHERE name = '%s' LIMIT 1", EscapeString(name));
return mysql_tquery(sql_connection, query, "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.
Re: Looking for an efficient way to verify the existance of an account -
SyS - 11.06.2018
You can send update query and check the affected rows count
PHP код:
/*Inside your ban command */
new query[75];
mysql_format(sql_connection, sizeof(query), "UPDATE player SET BANNED = 1 WHERE name = '%e'", name); //change the query according to your table structure
mysql_tquery(sql_connection, query, "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(whomexecuted, COLOR_RED, "This account does not exist.");
else
{
/*Ban the player...*/
SendClientMessage(whomexecuted, COLOR_RED, "Banned player successfully..");
return 1;
}
}
Re: Looking for an efficient way to verify the existance of an account -
Gammix - 11.06.2018
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'.
Re: Looking for an efficient way to verify the existance of an account -
Kane - 11.06.2018
A non threaded query for what you're trying to achieve. SyS's method may be better though.
Re: Looking for an efficient way to verify the existance of an account -
Robin96 - 11.06.2018
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?