Retrieving a value which can't be used twice.
#1

Hi

I have a row in my MySQL database that holds a unique value for each player. The players can choose this value themselves upon registration and I want to detect whether their input value is already in use or not.

I know how to select the field from my database but how can I then make an IF statement when the input value of the player is already in use or not.

Thanks in advance.
Reply
#2

Send a query to check if their value exists.

SELECT * FROM yourtable WHERE uniquevalue = enteredvalue

Use mysql_tquery so you can specify a callback.
Inside this callback, check if you got at least one row or none at all.
If the result didn't retreive a row (rows = 0), then the value isn't used.
If you got 1 row, the value is used and you should allow the player to choose another value again.

Which field are you actually talking about?
Your players might find it hard and annoying if you have 1000+ players registered, so in the worst case, they need to enter a value 1000 times to find an unused one.
Reply
#3

Alternatively add unique key to your field, then on insert check for error code 1586. This way only one query is ever executed, but you can't cache all already used keys (but on the bright side, you won't have to deal with race conditions when caching taken keys from select).
Reply
#4

Quote:
Originally Posted by PowerPC603
Посмотреть сообщение
Send a query to check if their value exists.

SELECT * FROM yourtable WHERE uniquevalue = enteredvalue

Use mysql_tquery so you can specify a callback.
Inside this callback, check if you got at least one row or none at all.
If the result didn't retreive a row (rows = 0), then the value isn't used.
If you got 1 row, the value is used and you should allow the player to choose another value again.
pawn Код:
forward SQL_Check_Value(playerid);
public SQL_Check_Value(playerid)
{
    new rows, fields;
    cache_get_data(rows, fields, mysql);
    if(rows)
    {
        /*
            Value already in use
            Show dialog
        */

    }
    else
    {
        /*
            Show dialog when the value isn't used yet.
        */

    }
    return true;
}
So I've got this but none of the dialogs show. I then checked the mysql_log and it showed me this:
Код:
[10:39:08] [DEBUG] mysql_tquery - connection: 1, query: "SELECT * FROM Users WHERE value = 'string' LIMIT 1", callback: "SQL_Check_Value", format: "i"
[10:39:08] [DEBUG] CMySQLQuery::Execute[SQL_Check_Value] - starting query execution
[10:39:08] [DEBUG] CMySQLQuery::Execute[SQL_Check_Value] - query was successfully executed within 17.746 milliseconds
[10:39:08] [DEBUG] CMySQLResult::CMySQLResult() - constructor called
[10:39:08] [DEBUG] Calling callback "SQL_Check_Value"..
[10:39:08] [DEBUG] CMySQLResult::~CMySQLResult() - deconstructor called
Reply
#5

Show your query
Reply
#6

pawn Код:
mysql_format(mysql, query, sizeof(query), "SELECT * FROM `playerdata` WHERE `value` = '%d'", enteredvalue);
new Cache:result = mysql_query(mysql, query);
if(cache_get_row_count() > 0)
{
    //if the row exists
}
else
{
    //if it doesn't
}
cache_delete(result);
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)