PDA

View Full Version : MySQL Question


IdonTmiss
15/07/2018, 12:00 PM
Well I want to make an "offunlockacc" command, that gets and then sets column "pLocked" to 0, while the user is offline


cmd:offunlockacc(playerid, params[])
{

}

IdonTmiss
15/07/2018, 01:59 PM
Anyone?

Iguman
15/07/2018, 02:38 PM
Example:

bool:IsPlayerOnline(playerid)
{
if(IsPlayerConnected(playerid) && IsPlayerLogged[playerid])
return true;
return false;
}
stock GetName(playerid)
{
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid, name, sizeof(name));
return name;
}

CMD:offunlockacc(playerid, params[])
{
new name[MAX_PLAYER_NAME];
if(sscanf(params, "s[24]", name)) return SCM(playerid, COLOR_RED, "USAGE: /offunlockacc [full player name]");
if(strlen(name) < 4 || strlen(name) > MAX_PLAYER_NAME) return SCM(playerid, COLOR_RED, "ERROR: Incorrect name length.");
for(new i = 0; i < MAX_PLAYERS; i++) // Better use foreach
{
if(IsPlayerOnline(i))
{
if(!strcmp(GetName(i), name))
{
SCM(playerid, COLOR_RED, "ERROR: That user is online.");
return 1;
}
}
}

new query[256], string[128];
mysql_format(Database, query, sizeof(query), "SELECT * FROM `Users` WHERE `Name`='%s' LIMIT 1;", name);
new Cache:result = mysql_query(Database, query);
if(cache_num_rows()) // User was found
{
mysql_format(Database, query, sizeof(query), "UPDATE `Users` SET `pLocked`='0' WHERE `Name`='%s';", name);
mysql_tquery(Database, query);

format(string, sizeof(string), "You have unlocked account: %s", name);
SCM(playerid, COLOR_GREEN, string);
}
else
{
format(string, sizeof(string), "ERROR: User with name ' %s ' couldn't be found in database.", name);
SCM(playerid, COLOR_RED, string);
}
cache_delete(result);
return 1;
}


Untested.

IdonTmiss
15/07/2018, 02:46 PM
Works, thanks a lot :)