How to check the next id in a mysql table?
#1

How could I.....

If I wanted to get the next id that's free in a table i.e

Someone purchases a vehicle from the dealer... How would I get the next MySQL ID in the table i.e

If the data stored before it was id 6....How could I do a query to know that the next id will be 7?
Reply
#2

Enable AUTO_INCREMENT for your index field. Also set it as primary key.
Reply
#3

Yes it is already set, but what I mean is, Alright:

In my players table.... There is 3 vehicle owned slots... the id of the vehicle(Mysql id) is stored in one of these 3 slots that way there is a seperate table for vehicles....

When a player purchases a vehicle it will be added to the table, how do I get the ID of the newly inserted row?
Reply
#4

You could use this I made awhile ago:

pawn Код:
stock gvi()
{
    new str[128], id;
    for(new i = 1; i < MAX_VEHICLES; i++)
    {
        format(str, sizeof(str),"SELECT * FROM `yourtable` WHERE `VehicleID` = '%i'", i);
        mysql_query(str);
        mysql_store_result();
        if(mysql_num_rows() == 0) {
            id = i;
            break;
        }
        mysql_free_result();
    }
    return id;
}
Reply
#5

Thanks!
Reply
#6

if you are using BlueG's mysql plugin, check the mysql_insert_id function.

copied from wiki:

Код:
mysql_insert_id

Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).
pawn Код:
mysql_query("INSERT INTO `players` (name,password) VALUES ('Ownage',MD5('mypass')");
printf("New player registered with ID %d",mysql_insert_id());
https://sampwiki.blast.hk/wiki/MySQL#mysql_insert_id
Reply
#7

Oh, in that case just use mysql_insert_id().
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)