[SQLite] search ban help needed
#1

So, I finally created a searchban command, but I can't get the "like" to work, or some other stuff :l
Here's the code;
pawn Код:
CMD:searchban(playerid, params[]) {
    new Query[240], string[128], NameOrIP[40], DBResult:Result;
    if(!PlayerInfo[playerid][AdminLevel]) return SendClientMessage(playerid, COLOR_ORANGE, "NOTE: You need to be an admin!");
    if(sscanf(params, "s[40]", NameOrIP)) return SendClientMessage(playerid, COLOR_NOTES, "USAGE: /searchban PLAYERNAME/PLAYERIP");
    format(Query, sizeof(Query), "SELECT * FROM `BANNED` WHERE `NAME` LIKE '%s' OR `IP` LIKE '%s' ORDER BY `DATE` DESC LIMIT 6", NameOrIP, NameOrIP);
    Result = db_query(HDF, Query);
    if(db_num_rows(Result))
    {
        new BannedBy[24], BannedName[24], BannedIP[24], BannedReason[50];
        do
        {
            db_get_field_assoc(Result, "NAME", BannedName, sizeof(BannedName));
            db_get_field_assoc(Result, "IP", BannedIP, sizeof(BannedIP));
            db_get_field_assoc(Result, "ADMIN", BannedBy, sizeof(BannedBy));
            db_get_field_assoc(Result, "REASON", BannedReason, sizeof(BannedReason));

            format(string, sizeof(string), "- %s(IP: %s) - banned by %s - due to %s", BannedName, BannedIP, BannedBy, BannedReason);
            SendClientMessage(playerid, COLOR_NOTES2, string);
        }
    }
    else SendClientMessage(playerid, COLOR_ORANGE, "NOTE: No bans found!");
    db_free_result(Result); string = "\0", Query = "\0", NameOrIP = "\0";
    return 1;
}
It does show me the result if I fully type the name, but It doesn't show me the next results.
Example;
I ban "Train" and "Train_Super"
If I type /searchban Train, It will only show me the result for "Train", but not "Train_Super".
I've used LIKE '%%s%", but then it didn't work at all d:
Reply
#2

pawn Код:
CMD:searchban(playerid, params[])
{
    if(!PlayerInfo[playerid][AdminLevel]) SendClientMessage(playerid, COLOR_ORANGE, "NOTE: You need to be an admin!");
    else if(isnull(params)) SendClientMessage(playerid, COLOR_NOTES, "USAGE: /searchban PLAYERNAME/PLAYERIP");
    else{
        new Query[240], string[145];
        format(Query, sizeof(Query), "SELECT * FROM `BANNED` WHERE `NAME` LIKE '%s' OR `IP` LIKE '%s' ORDER BY `DATE` DESC LIMIT 6", params, params);
        new DBResult:Result = db_query(HDF, Query);
        if(db_num_rows(Result))
        {
            new BannedBy[MAX_PLAYER_NAME], BannedName[MAX_PLAYER_NAME], BannedIP[MAX_PLAYER_NAME], BannedReason[MAX_PLAYER_NAME*2];
            do
            {
                db_get_field_assoc(Result, "NAME", BannedName, sizeof(BannedName));
                db_get_field_assoc(Result, "IP", BannedIP, sizeof(BannedIP));
                db_get_field_assoc(Result, "ADMIN", BannedBy, sizeof(BannedBy));
                db_get_field_assoc(Result, "REASON", BannedReason, sizeof(BannedReason));

                format(string, sizeof(string), "- %s(IP: %s) - banned by %s - due to %s", BannedName, BannedIP, BannedBy, BannedReason);
                SendClientMessage(playerid, COLOR_NOTES2, string);
            }
            while(db_next_row(Result));
        }
        else SendClientMessage(playerid, COLOR_ORANGE, "NOTE: No bans found!");
        db_free_result(Result);
    }
    return 1;
}
Reply
#3

Quote:
Originally Posted by Jefff
Посмотреть сообщение
pawn Код:
CMD:searchban(playerid, params[])
{
    if(!PlayerInfo[playerid][AdminLevel]) SendClientMessage(playerid, COLOR_ORANGE, "NOTE: You need to be an admin!");
    else if(isnull(params)) SendClientMessage(playerid, COLOR_NOTES, "USAGE: /searchban PLAYERNAME/PLAYERIP");
    else{
        new Query[240], string[145];
        format(Query, sizeof(Query), "SELECT * FROM `BANNED` WHERE `NAME` LIKE '%s' OR `IP` LIKE '%s' ORDER BY `DATE` DESC LIMIT 6", params, params);
        new DBResult:Result = db_query(HDF, Query);
        if(db_num_rows(Result))
        {
            new BannedBy[MAX_PLAYER_NAME], BannedName[MAX_PLAYER_NAME], BannedIP[MAX_PLAYER_NAME], BannedReason[MAX_PLAYER_NAME*2];
            do
            {
                db_get_field_assoc(Result, "NAME", BannedName, sizeof(BannedName));
                db_get_field_assoc(Result, "IP", BannedIP, sizeof(BannedIP));
                db_get_field_assoc(Result, "ADMIN", BannedBy, sizeof(BannedBy));
                db_get_field_assoc(Result, "REASON", BannedReason, sizeof(BannedReason));

                format(string, sizeof(string), "- %s(IP: %s) - banned by %s - due to %s", BannedName, BannedIP, BannedBy, BannedReason);
                SendClientMessage(playerid, COLOR_NOTES2, string);
            }
            while(db_next_row(Result));
        }
        else SendClientMessage(playerid, COLOR_ORANGE, "NOTE: No bans found!");
        db_free_result(Result);
    }
    return 1;
}
Still only shows one result ( If I type /searchban Train, it will only show "Train" and not "Train..."
Reply
#4

Make sure you insert a literal %% to indicate a wildcard.
Reply
#5

Quote:
Originally Posted by Pottus
Посмотреть сообщение
There seems to be some bug when inserting a literal % where you need to do it like this %%%% yes I know it says to do %% but try this code here.
Wildcards indeed.

SQL LIKE accepts wildcards, the one you want is '%', this tells the engine that there can be 0 or more missing letters there.
%string% will give you what you need.
Samp escapes the % sigh so you'll need to use %%
Код:
SELECT * FROM `BANNED` WHERE `NAME` LIKE '%%%s%%' OR `IP` LIKE '%s' ORDER BY `DATE` DESC LIMIT 6
Reply
#6

Quote:
Originally Posted by Pottus
Посмотреть сообщение
Make sure you insert a literal %% to indicate a wildcard.
Quote:
Originally Posted by FUNExtreme
Посмотреть сообщение
Wildcards indeed.

SQL LIKE accepts wildcards, the one you want is '%', this tells the engine that there can be 0 or more missing letters there.
%string% will give you what you need.
Samp escapes the % sigh so you'll need to use %%
Код:
SELECT * FROM `BANNED` WHERE `NAME` LIKE '%%%s%%' OR `IP` LIKE '%s' ORDER BY `DATE` DESC LIMIT 6
Thanks, I guess I wasn't using enough %'s previously.
+rep'd everyone I could
EDIT: managed to +rep everyone
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)