#Just_Me Posted August 29, 2016 Share Posted August 29, 2016 hello guys .. i was trying to make a mod that depends on the ID like i want when i create a vehicle .. it have its own id .. and this id be saved on SQL so what i was trying to do is to check if there is a vlid id number to put it on the next vehicle that will be created. like if i created a vehicle lets say the id will be: 1 so if i created another vehicle the id will be: 2 but if i removed the first vehicle and created a new one the id will be: 1 cause it's a valid number on the SQL .. so i did it like that but the problem it gets the first valid number like if i repeated this code again lets say the valid id number was: 10 so if i repeated the code again to check for a valid numbers it will be: 10 i don't know what's wrong with it. My codes: function getValidID () local id = 0 for k,v in ipairs (getDatas ()) do if tonumber (v.ID) ~= k then id = k break else id = #getDatas () + 1 end end return id end function getDatas () local poll = dbQuery (db, "SELECT * FROM Vehicles") local data = dbPoll (poll, -1) if (type (data) == "table" and #data == 0) or not data then return { } else return data end end Link to comment
idarrr Posted August 29, 2016 Share Posted August 29, 2016 Use AUTO_INCREMENT on MySQL MySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature. By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record. The SQL statement below would insert a new record into the table. The "ID" column would be assigned a unique value. Example: CREATE TABLE `vehicles` ( `id` INT(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) Or if you already have your own table, just set the default value to NOT NULL and AUTO_INCREMENT and set it as PRIMARY KEY. It will automatically generated new ID. Link to comment
#Just_Me Posted August 29, 2016 Author Share Posted August 29, 2016 uhmm .. i mean is there a way with codes ? like the code above ? Link to comment
Walid Posted August 29, 2016 Share Posted August 29, 2016 uhmm .. i mean is there a way with codes ? like the code above ? Try sth like this function getValidID() local result = dbPoll(dbQuery(db, "SELECT ID FROM Vehicles ORDER BY ID ASC"), -1) newID = false for i, id in pairs (result) do if id["ID"] ~= i then newID = i break end end if newID then return newID else return #result + 1 end end Link to comment
#Just_Me Posted August 29, 2016 Author Share Posted August 29, 2016 that didn't work too # same problem repeating the first valid number if the valid number that shows to me has been taken it still a valid number .. like 2 if it's no longer valid it will show up again 2 Link to comment
idarrr Posted August 29, 2016 Share Posted August 29, 2016 that didn't work too #same problem repeating the first valid number if the valid number that shows to me has been taken it still a valid number .. like 2 if it's no longer valid it will show up again 2 That code above should work, it loops all the ID on DB and compare it with index, maybe the reason why the ID show up again is because you didn't insert the ID 2(after you get ID 2 as Valid ID) to database, so ID 2 still not exist on database and it show up again as valid ID. Link to comment
#Just_Me Posted August 29, 2016 Author Share Posted August 29, 2016 no i checked the database if that number wasn't there or something like that but the number was already there .. but the problem is it still doesn't working correctly it keeps repeating the Non-Valid number as Valid number i don't know why .. Link to comment
#Just_Me Posted August 30, 2016 Author Share Posted August 30, 2016 Solved .. i found out another way ... thanks all. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now