nathanmaxsonadil Posted August 31, 2007 Share Posted August 31, 2007 Hi, I was wondering how to get the id that has the biggest number and then add 1 to it so I can have an id field id name 1 test 2 test1 Quote Link to comment Share on other sites More sharing options...
matthewhaworth Posted August 31, 2007 Share Posted August 31, 2007 If you're using MySQL, though I'm sure it's possible in other DBMS... you can add something named 'auto_increment' onto the field and the database deals with that for you. Quote Link to comment Share on other sites More sharing options...
nathanmaxsonadil Posted August 31, 2007 Author Share Posted August 31, 2007 thanks Quote Link to comment Share on other sites More sharing options...
nathanmaxsonadil Posted August 31, 2007 Author Share Posted August 31, 2007 how would I do that? I searched in google bu no luck. Quote Link to comment Share on other sites More sharing options...
nathanmaxsonadil Posted August 31, 2007 Author Share Posted August 31, 2007 never mind found it in the extra tab in phpmyadmin Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 31, 2007 Share Posted August 31, 2007 If u have phpmyadmin (which u should have) edit the id column, go to extras and select auto_increment. EDIT: didnt see found it by yourself Quote Link to comment Share on other sites More sharing options...
nathanmaxsonadil Posted August 31, 2007 Author Share Posted August 31, 2007 ALTER TABLE `mytbtest` CHANGE `id` `id` VARCHAR( 255 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL AUTO_INCREMENT MySQL said: Documentation #1063 - Incorrect column specifier for column 'id' Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 31, 2007 Share Posted August 31, 2007 Wow...you just love to close and open this topic. Just do this to save all the troubles: In your script where you want to insert the id, put this $query = mysql_query("SELECT * FROM table"); $row = mysql_num_rows($query) + 1; $row will have the next available id number. Quote Link to comment Share on other sites More sharing options...
nathanmaxsonadil Posted August 31, 2007 Author Share Posted August 31, 2007 k thanks Quote Link to comment Share on other sites More sharing options...
matthewhaworth Posted August 31, 2007 Share Posted August 31, 2007 No, don't take the easy way out, it'll prove terribly inefficient. What if you delete a record so you only have 6 records but the latest id is 7? It'll try to add 7 again and result in an error. http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html phpMyAdmin should deal with it for you. It works best for primary keys, and I'm assuming iD will be your primary key . Quote Link to comment Share on other sites More sharing options...
nathanmaxsonadil Posted August 31, 2007 Author Share Posted August 31, 2007 thanks I had to make it a primary... Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 31, 2007 Share Posted August 31, 2007 ALTER TABLE `mytbtest` CHANGE `id` `id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY EDIT: lol im always posting after u get solve it by yourself :-\ Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 31, 2007 Share Posted August 31, 2007 Well for my method, it's not hard to fix the id numbers after you deleted something. A for/while loop should fix everything. It's like re-updating the id column. Quote Link to comment Share on other sites More sharing options...
matthewhaworth Posted August 31, 2007 Share Posted August 31, 2007 Well for my method, it's not hard to fix the id numbers after you deleted something. A for/while loop should fix everything. It's like re-updating the id column. What if you had thousands of users? It's a big request on the server every time someone deletes an account. Also, a solution has already been provided my DBMS's, so 'why reinvent the wheel'. I hear that said all over these forums. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 31, 2007 Share Posted August 31, 2007 What if you had thousands of users? It's a big request on the server every time someone deletes an account. Also, a solution has already been provided my DBMS's, so 'why reinvent the wheel'. I hear that said all over these forums. Point well made. Quote Link to comment Share on other sites More sharing options...
nathanmaxsonadil Posted August 31, 2007 Author Share Posted August 31, 2007 also if 2 people went to the page at the same time. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.