MySQL -- Free ID Slot
#1

Ello!

Been trying to figure out how MySQL works, kinda got the hang of it untill i stumbled across a problemo!

PHP код:
Vehicle ID 1 Owner BLBLA
Vehicle ID 4 
Owner BLBLA
Vehicle ID 5 
Owner BLBLA
Vehicle ID 7 
Owner BLBLA 
Is there any way to find a free Vehicle ID slot? In the example above : ID 2, ID 3, and ID 6. These would be created in order. I seriously have no idea what I have to do.

Yoran.
Reply
#2

If you want to insert a unique ID into the database use this code:

pawn Код:
mysql_insert_id
I hope this is what you mean.

EDIT: You could find an example on how this is used on SA-MP wiki (https://sampwiki.blast.hk/wiki/MySQL)
Reply
#3

Search about AUTO_INCREMENT and use mysql_insert_id or cache_insert_id (if you're using the latest release of BlueG's plugin) and just SELECT your data from the table and put the vehicle IDs and names in a/an variable/array.
Reply
#4

PHP код:
[21:08:52CMySQLHandler::Query(INSERT INTO Vehicles (VehicleIDVehicleOwnerVALUES ('1''Bitch')) - An error has occured. (Error ID1062Duplicate entry '1' for key 'PRIMARY')
[
21:08:52] >> mysql_insert_idConnection handle
PHP код:
COMMAND:test(playeridparams[])
{
    new 
query[500];
    
format(querysizeof(query), "INSERT INTO Vehicles (VehicleID, VehicleOwner) VALUES ('%d', 'Bitch')"mysql_insert_id());
    
mysql_query(query);
    
printf("ID %d"mysql_insert_id());
    return 
1;

Reply
#5

No not like that! If you're having AUTO_INCREMENT, you don't have to specify VehicleID. It will auto increment if you insert the data.

pawn Код:
COMMAND:test(playerid, params[])
{
        new query[500]; // what the hell 500?
        mysql_query("INSERT INTO Vehicles (VehicleOwner) VALUES ('Bitch')");
        return 1;
}
Reply
#6

If you delete a vehicle from the database and you retype the command, it won't refill the deleted vehicle slot... It would just increase where it left off at.
Reply
#7

Quote:
Originally Posted by yoran765
Посмотреть сообщение
If you delete a vehicle from the database and you retype the command, it won't refill the deleted vehicle slot... It would just increase where it left off at.
If you need to delete the one vehicle field, you can simply change AUTO_INCREMENT value by doing (send this query through phpMyAdmin).

PHP код:
ALTER TABLE `tableAUTO_INCREMENT '0'/* change 0 to your last VehicleID (after removing one of them) */ 
The above query resets AUTO_INCREMENT value to 0.
Reply
#8

After doing so it resets the value of A_I. But what if you have deleted id 2 and you now have id 1 and 3 the next id would be 2. typing the cmd would make the next id... 3 right?
Reply
#9

Quote:
Originally Posted by yoran765
Посмотреть сообщение
After doing so it resets the value of A_I. But what if you have deleted id 2 and you now have id 1 and 3 the next id would be 2. typing the cmd would make the next id... 3 right?
If you don't have ID 2 and you already have ID 1 and 3, it'll start from ID 4, not from ID 2 again because the value is already incremented.
Reply
#10

Aha! Is there anyway to reset the value of AUTO_INCREMENT without using PHPMYADMIN?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)