MySQL -- Get VehicleID
#1

Hello again!

So i've been experimenting with MySQL for a while now but I still can't figure something out. I'm currently storing VehicleID's into a database. For example i've stored these :
PHP код:
1
2
3
4

When I come ingame they all load and work perfectly fine, although when I delete for example :
PHP код:
2

The database looks like this :
PHP код:
1
3

After restarting the server they get loaded! BUT the samp vehicle ids will become like this :
PHP код:
1
2

Meaning I can't get the current vehicleID they're in. Is there anyway of returning the vehicleid they're currently in?

Yoran
Reply
#2

Maybe you have created loop in your saving query which inserts these wrong numbers
Reply
#3

i don't see any problem here... but you have a problem to understand the samp...
let think this situation... you have a server that can store 10 vehicles...

This is how the list with vehicle look when the server start
Код:
1. none
2. none
3. none
4. none
5. none
6. none
7. none
8. none
9. none
10. none
You add a vehicle "Infernus" to server, he will receive the first value that is free
Код:
1. "Infernus"
2. none
3. none
4. none
5. none
6. none
7. none
8. none
9. none
10. none
You add a new vehicle "Banshee" to server, he will receive the first value that is free
Код:
1. "Infernus"
2. "Banshee"
3. none
4. none
5. none
6. none
7. none
8. none
9. none
10. none
You add a new vehicle "Hotknife" to server, he will receive the first value that is free
Код:
1. "Infernus"
2. "Banshee"
3. "Hotknife"
4. none
5. none
6. none
7. none
8. none
9. none
10. none
You add a new vehicle "Admiral" to server, he will receive the first value that is free
Код:
1. "Infernus"
2. "Banshee"
3. "Hotknife"
4. "Admiral"
5. none
6. none
7. none
8. none
9. none
10. none
Let say you want to delete a vehicle '2. "Banshee"' for example, you will get this
Код:
1. "Infernus"
2. none
3. "Hotknife"
4. "Admiral"
5. none
6. none
7. none
8. none
9. none
10. none
You add a new vehicle "Seasparrow" to server, he will receive the first value that is free (in this case will be 2)
Код:
1. "Infernus"
2. "Seasparrow"
3. "Hotknife"
4. "Admiral"
5. none
6. none
7. none
8. none
9. none
10. none
Let say you want to delete a vehicle 2. "Seasparrow" & '3. "Hotknife"' for example, you will get this
Код:
1. "Infernus"
2. none
3. none
4. "Admiral"
5. none
6. none
7. none
8. none
9. none
10. none
In this moment you save all the data for vehicle in you databse (1) & (4)... and restart the server

When the server start you will add the vehicles...

Add the first vehicle from database (1), id 1 looks good...
Код:
1. "Infernus"
2. none
3. none
4. none
5. none
6. none
7. none
8. none
9. none
10. none
Add the second vehicle from database (4)... in this moment he will recive the id 2 and not 4 (the first id free is taken, the samp will never put the same id before the restart)
Код:
1. "Infernus"
2. "Admiral"
3. none
4. none
5. none
6. none
7. none
8. none
9. none
10. none
I hope you got the point...
Reply
#4

Ooh god that's exactly how I explained it lol. Only problem is that I can't get the stored vehicle ID from the database, since the vehicle ID of samp defers from the ID stored. Therefore I can not use GetPlayerVehicleID
Reply
#5

Stupid and not how MySQL works at all. You need to assign the unique, non-changing MySQL key to the ingame volatile vehicleid, not the other way round. Add a variable to your enum, or whatever you use to store data;
pawn Код:
e_vehicleSQLID
Or something like that. Then load that key like you would any other value. When you want to update the vehicle, use that key rather than the vehicleid.
Reply
#6

I feel dumb and retarded now... Thank you Vince!
Reply
#7

I have no idea how MySQL works and i'm getting pretty frustrated. What do you mean by MySQL non changing key to the ingame volatile vehicleid. You can't create your own ingame vehicleid, those go by order or am I wrong?.
Reply
#8

Quote:
Originally Posted by yoran765
Посмотреть сообщение
I have no idea how MySQL works and i'm getting pretty frustrated. What do you mean by MySQL non changing key to the ingame volatile vehicleid. You can't create your own ingame vehicleid, those go by order or am I wrong?.
He means if you load your vehicle you add something like this;
pawn Код:
enum vehicleinfo
{
    SQLID,
    VehicleID,
    Owner[MAX_PLAYER_NAME]
}
new VehicleInfo[MAX_VEHICLES][vehicleinfo];
And then when loading the vehicle (Alltough I always use(d) StrickenKid's SQL plugin and don't know what you use) you do something like this;

pawn Код:
public LoadVehicles()
{
    ...
    new string[128];
    format(string, sizeof(string), "...", ...);
    mysql_query(string);
    mysql_store_result();
    ...
    mysql_fetch_field("SQLID", string);
    VehicleInfo[i][SQLID] = strval(string);
    VehicleInfo[i][VehicleID] = CreateVehicle(...);
    mysql_free_result();
    return 1;
}
Then when you want to save to your SQL database for that vehicle you do something like this:
pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    if(!ispassenger)
        {
        for(new i = 0; i < MAX_VEHICLES; i++)
            {
            if(VehicleInfo[i][VehicleID] == vehicleid)
                {
                new string[128], name[MAX_PLAYER_NAME];
                GetPlayerName(playerid, name, sizeof(name));
                strmid(VehicleInfo[i][Owner], name, 0, strlen(name), MAX_PLAYER_NAME);
                format(string, sizeof(string), "UPDATE `Vehicles` SET `Owner` = '%s' WHERE `SQLID` = %d", VehicleInfo[i][Owner], VehicleInfo[vehicleid][SQLID]);
                mysql_query(string);
                }
            }
        }
    return 1;
}
It's mostly about the 'WHERE `SQLID` = %d' part but I hope you understand what I mean!

Best regards,
Jesse
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)