MySQL help
#1

I've got this function:
pawn Код:
stock GivePlayerPhone(playerid)
{
    new Cache:result = mysql_query(g_SQL, "SELECT COUNT(*) FROM `players`");
    new players;
    cache_get_value_int(0, 0, players);
    if(players == 1)
    {
        Player[playerid][Phone] = 42455;
        cache_delete(result);
    }
    else
    {
        new Cache:lastPhone =  mysql_query(g_SQL, "SELECT 'phone' FROM `players` ORDER BY  MAX(id) DESC LIMIT 1)");
        new phone;
        cache_get_value_int(0, 0, phone);
        Player[playerid][Phone] = phone + 1;
        cache_delete(lastPhone);
    }
}
Basically what it does is it gives a player a phone number, the first player to buy a phone gets the number 42455, and from there it should increase (42456, 42457, etc..). I got the first phone number working, but the second part of the code isn't working, in the database it just saves the row ID in the phone field instead of the increasing phone number. So what would be the correct query to use for this kind of thing?

P.S. if there is a simpler or more efficient way to do this please tell me, I suck with mysql stuff

Thanks in advance.
Reply
#2

I will start by pointing out that the second query is syntactically correct but logically wrong. You are selecting the literal string "phone" rather than the value in the column.

You could also write it much simpler just by adding 42455 and the player count together. If the count is 0 then the number will be 42455, if the count is 1 it will be 42456, and so on. Don't even need any other queries.
Reply
#3

Quote:
Originally Posted by Vince
Посмотреть сообщение
I will start by pointing out that the second query is syntactically correct but logically wrong. You are selecting the literal string "phone" rather than the value in the column.

You could also write it much simpler just by adding 42455 and the player count together. If the count is 0 then the number will be 42455, if the count is 1 it will be 42456, and so on. Don't even need any other queries.
I was trying to figure out how to select the value.

The thing is if a player doesn't buy a phone before the next player it could lead to two or more players having the same phone number.
Reply
#4

You could add 42454 to user id so all players would get unique phone number.(i assume that user id is unique)
Reply
#5

Quote:
Originally Posted by Spmn
Посмотреть сообщение
You could add 42454 to user id so all players would get unique phone number.(i assume that user id is unique)
I guess that could work too, I thought of this though:
Код:
SELECT COUNT(*) FROM `players` WHERE `phone` > 0
phone is set to 0 in the db if you haven't bought one.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)