how to check if exists?
#1

Hi i use mysql and i want to check if my randomly generated code already exists and if so, it compiles a new one untill it not exists!


pawn Код:
stock GeneratePhoneNumber(playerid)
{

    PLAYERS[playerid][Number] = 10000 + random(89999);
    return 1;

}
how to make that check?
Reply
#2

You need to load information from the DB and store the results of the column in which phone numbers are stored. You can then use sscanf to extract the data and save it into a multi-dimensional variable (something like SavedNumbers[FIELD_RESULTS_AMOUNT], where "FIELD_RESULTS_AMOUNT" is the amount of fields returned by the query). You will then need to check to see if the generated phone number matches any of the fields stored into the variables, if the numbers match, execute the "GeneratePhoneNumber" function again. If it doesn't, free the query results, delete the variable values, and store the new phone number.
Reply
#3

may this work?

pawn Код:
stock GeneratePhoneNumber(playerid)
{


            PLAYERS[playerid][Number] = 10000 + random(89999);
            do
            {
                PLAYERS[playerid][Number] = 10000 + random(89999);
                new Query[256];
                format(Query, sizeof(Query), "SELECT * FROM `PINFO` WHERE `Number` = %i LIMIT 1", PLAYERS[playerid][Number]);
                mysql_query(Query); //Queries
                mysql_store_result();

            }
            while mysql_num_rows() == 0;
            mysql_free_result();
            return 1;

}
Reply
#4

It seems too complicated to be honest with you. I don't think you are using "do" and "while" correctly though, either.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)