Clan command shows ID 0 instead of anything else
#1

Hello, so I've been trying to re-create a clan system, and I'm facing an error...

Here's the code:

PHP код:
CMD:createclan(cmdidplayeridparams[])
{
    if(!
UserStats[playerid][Clan])
    {
        new 
clanname[32], query[300];
        if(
sscanf(params"s[32]"clanname)) return SendClientMessage(playeridCOLOR_RED"[USAGE]: /createclan [clanname]");
        
mysql_format(Databasequerysizeof(query), "SELECT * FROM `clans` WHERE `ClanName` = '%e'"clanname);
        new 
Cache:result mysql_query(Databasequery);
        if(
cache_num_rows())
        {
            
SendClientMessageEx(playeridCOLOR_RED"[ERROR]: Clan name ''%s'' already exists. Pick another name."clanname);
        }
        else
        {
            
mysql_format(Databasequerysizeof(query), "INSERT INTO `clans` (`ClanName`, `ClanLeader`, `Official`) VALUES ('%e', '%e', '0')"clannameGetName(playerid));
            
mysql_tquery(Databasequery);
            
UserStats[playerid][Clan] = cache_insert_id();
            new 
clanid UserStats[playerid][Clan];
            
ClanInfo[clanid][ClanID] = clanid;
            
ClanInfo[clanid][ClanName] = clanname;
            
ClanInfo[clanid][ClanLeader] = GetName(playerid);
            
ClanInfo[clanid][Official] = 0;
            
cache_delete(result);
        }
    }
    else
    {
        
SendClientMessage(playeridCOLOR_RED"[ERROR]: You already own a clan.");
    }
    return 
1;

And then when I do /myclan ingame

PHP код:
CMD:myclan(cmdidplayeridparams[])
{
    if(!
UserStats[playerid][Clan])
    {
        
SendClientMessage(playeridCOLOR_WHITE"You are not in a clan.");
    }
    else
    {
        
SendClientMessageEx(playeridCOLOR_WHITE"Your current clan ID is: %d"UserStats[playerid][Clan]);
    }
    return 
1;

It just shows Your current clan ID is: 0 while it should be 1 or 2..
Reply
#2

I recommend using threaded queries, you weren't storing the query cache result, therefore it returned 0 when you called cache_insert_id();

PHP код:
mysql_tquery(Databasequery"query_insertIntoClans""d.."playerid);
forward query_insertIntoClans(playerid, ..);
public 
query_insertIntoClans(playerid, ..) {
    
UserStats[playerid][Clan] = cache_insert_id();
    return 
1;

Reply
#3

Quote:
Originally Posted by ******
Посмотреть сообщение
What is `SendClientMessageEx`? The fact that it shows `0` in an `else` branch that just checked it wasn't 0 implies something else is going on.
It's like formatting a string and sending the message to a player, it's just made by Emmet like that
Reply
#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
#5

Hello, thanks to everyone who replied above. I've fixed the issue I had, but now I ran into some other problem.

So, this:

PHP код:
forward OnClanCreate(playeridclanname[]);
public 
OnClanCreate(playeridclanname[])
{
    if(
cache_affected_rows())
    {
        new 
clanid UserStats[playerid][Clan] = cache_insert_id();
        
ClanInfo[clanid][ClanID] = clanid;
        
ClanInfo[clanid][ClanName] = clanname;
        
ClanInfo[clanid][ClanLeader] = GetName(playerid);
        
ClanInfo[clanid][Official] = 0;
        
SendClientMessageEx(playeridCOLOR_RED"[SUCCESS]: (Unofficial) Clan ID %d with the name %s has been created."clanidclanname);
    }
    else
    {
        
SendClientMessageEx(playeridCOLOR_RED"[ERROR]: Clan name ''%s'' already exists. Pick another name."clanname);
    }
    return 
1;

Line:

PHP код:
ClanInfo[clanid][ClanName] = clanname
PHP код:
error 047: array sizes do not match, or destination array is too small 
Reply
#6

I did mention about it in my post:
Quote:
Originally Posted by Calisthenics
Посмотреть сообщение
Do not copy strings directly like this:
pawn Код:
ClanInfo[clanid][ClanName] = clanname;
ClanInfo[clanid][ClanLeader] = GetName(playerid);
use `strcpy` macro.
Reply
#7

Never used strcpy before, can you give me an example to that?
Reply
#8

Ok, I've added this on my script:

PHP код:
stock strcpy(dest[], const source[], maxlength=sizeof dest)
{
    
strcat((dest[0] = EOSdest), sourcemaxlength);

Now, how would I use it?

pawn Код:
forward OnClanCreate(playerid, clanname[]);
public OnClanCreate(playerid, clanname[])
{
    if(cache_affected_rows())
    {
        new clanid = UserStats[playerid][Clan] = cache_insert_id();
        ClanInfo[clanid][ClanID] = clanid;
        strcpy(clanname, sizeof, clanname))
        ClanInfo[clanid][ClanLeader] = GetName(playerid);
        ClanInfo[clanid][Official] = 0;
        SendClientMessageEx(playerid, COLOR_RED, "[SUCCESS]: (Unofficial) Clan ID %d with the name %s has been created.", clanid, clanname);
    }
    else
    {
        SendClientMessageEx(playerid, COLOR_RED, "[ERROR]: Clan name ''%s'' already exists. Pick another name.", clanname);
    }
    return 1;
}
PHP код:
strcpy(clannamesizeofclanname)) 
or?
Reply
#9

Make it a macro:
pawn Код:
#if !defined strcpy
    #define strcpy(%0,%1) strcat((%0[0] = '\0', %0), %1)
#endif
First parameter is the destination string which is `ClanInfo[clanid][ClanName]`
Second parameter is the source which is `clanname`
Third parameter is the length which is optional unless it is an enum-array.
pawn Код:
strcpy(ClanInfo[clanid][ClanName], clanname, 32);
Reply
#10

Quote:
Originally Posted by Calisthenics
Посмотреть сообщение
Make it a macro:
pawn Код:
#if !defined strcpy
    #define strcpy(%0,%1) strcat((%0[0] = '\0', %0), %1)
#endif
First parameter is the destination string which is `ClanInfo[clanid][ClanName]`
Second parameter is the source which is `clanname`
Third parameter is the length which is optional unless it is an enum-array.
pawn Код:
strcpy(ClanInfo[clanid][ClanName], clanname, 32);
Alright now everything is fine, thanks to you and everyone else.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)