SA-MP Forums Archive
How to check the next id in a mysql table? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: How to check the next id in a mysql table? (/showthread.php?tid=316359)



How to check the next id in a mysql table? - Dokins - 06.02.2012

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?


Re: How to check the next id in a mysql table? - Vince - 06.02.2012

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


Re: How to check the next id in a mysql table? - Dokins - 06.02.2012

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?


Re: How to check the next id in a mysql table? - FuTuяe - 06.02.2012

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;
}



Re: How to check the next id in a mysql table? - Dokins - 06.02.2012

Thanks!


Re: How to check the next id in a mysql table? - [Diablo] - 06.02.2012

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


Re: How to check the next id in a mysql table? - Vince - 06.02.2012

Oh, in that case just use mysql_insert_id().