22.10.2018, 15:25
The problem is no active cache. You need to retrieve insert id in a specified callback of `mysql_tquery` function.
Extra notes:
Why do you select everything? A better method would be using COUNT(*) which returns 1 row and 1 column (0 or 1 in your case). An even better method is just 1 INSERT query.
Set `ClanName` column as UNIQUE KEY. Now you execute normally your query with `IGNORE` keyword:
You can set `Official` as default value 0 in table structure too.
It will execute the query and affected rows will be:
0) a clan with this name exists
1) all is OK, new row was inserted.
Do not copy strings directly like this:
use `strcpy` macro.
Extra notes:
pawn Код:
mysql_format(Database, query, sizeof(query), "SELECT * FROM `clans` WHERE `ClanName` = '%e'", clanname);
Set `ClanName` column as UNIQUE KEY. Now you execute normally your query with `IGNORE` keyword:
pawn Код:
mysql_format(Database, query, sizeof(query), "INSERT IGNORE INTO `clans` (`ClanName`, `ClanLeader`, `Official`) VALUES ('%e', '%e', '0')", clanname, GetName(playerid));
mysql_tquery(Database, query, "OnClanCreate", "ds", playerid, clanname);
It will execute the query and affected rows will be:
0) a clan with this name exists
1) all is OK, new row was inserted.
pawn Код:
forward OnClanCreate(playerid, clanname[]);
public OnClanCreate(playerid, clanname[])
{
if (cache_affected_rows())
{
new clanid = UserStats[playerid][Clan] = cache_insert_id();
...
}
else
{
SendClientMessageEx(playerid, COLOR_RED, "[ERROR]: Clan name ''%s'' already exists. Pick another name.", clanname);
}
}
pawn Код:
ClanInfo[clanid][ClanName] = clanname;
ClanInfo[clanid][ClanLeader] = GetName(playerid);