Clan command shows ID 0 instead of anything else
#4

The problem is no active cache. You need to retrieve insert id in a specified callback of `mysql_tquery` function.

Extra notes:
pawn Код:
mysql_format(Database, query, sizeof(query), "SELECT * FROM `clans` WHERE `ClanName` = '%e'", clanname);
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:
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);
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.

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);
    }
}
Do not copy strings directly like this:
pawn Код:
ClanInfo[clanid][ClanName] = clanname;
ClanInfo[clanid][ClanLeader] = GetName(playerid);
use `strcpy` macro.
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)