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 Link to comment https://forums.phpfreaks.com/topic/67501-solved-how-to-check-id/ 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. Link to comment https://forums.phpfreaks.com/topic/67501-solved-how-to-check-id/#findComment-338906 Share on other sites More sharing options...
nathanmaxsonadil Posted August 31, 2007 Author Share Posted August 31, 2007 thanks Link to comment https://forums.phpfreaks.com/topic/67501-solved-how-to-check-id/#findComment-338908 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. Link to comment https://forums.phpfreaks.com/topic/67501-solved-how-to-check-id/#findComment-338910 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 Link to comment https://forums.phpfreaks.com/topic/67501-solved-how-to-check-id/#findComment-338912 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 Link to comment https://forums.phpfreaks.com/topic/67501-solved-how-to-check-id/#findComment-338913 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' Link to comment https://forums.phpfreaks.com/topic/67501-solved-how-to-check-id/#findComment-338916 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. Link to comment https://forums.phpfreaks.com/topic/67501-solved-how-to-check-id/#findComment-338919 Share on other sites More sharing options...
nathanmaxsonadil Posted August 31, 2007 Author Share Posted August 31, 2007 k thanks Link to comment https://forums.phpfreaks.com/topic/67501-solved-how-to-check-id/#findComment-338921 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 . Link to comment https://forums.phpfreaks.com/topic/67501-solved-how-to-check-id/#findComment-338923 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... Link to comment https://forums.phpfreaks.com/topic/67501-solved-how-to-check-id/#findComment-338927 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 :-\ Link to comment https://forums.phpfreaks.com/topic/67501-solved-how-to-check-id/#findComment-338928 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. Link to comment https://forums.phpfreaks.com/topic/67501-solved-how-to-check-id/#findComment-338932 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. Link to comment https://forums.phpfreaks.com/topic/67501-solved-how-to-check-id/#findComment-338934 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. Link to comment https://forums.phpfreaks.com/topic/67501-solved-how-to-check-id/#findComment-338940 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. Link to comment https://forums.phpfreaks.com/topic/67501-solved-how-to-check-id/#findComment-338955 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.