daneth1712 Posted October 5, 2009 Share Posted October 5, 2009 Hi, I need to add into my database a field that inserts consecutive numbers starting from a 8 digit number I have selected. I need to be able to add the next number automatically to each row in the database (there are over 100,000) and then add some php code so that when a new entry is added, it adds the next number to the new record. The problem is I already have an auto increment field in the database, and am not sure how to do this. Can someone please help me out? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/176543-adding-auto-numbers/ Share on other sites More sharing options...
kickstart Posted October 5, 2009 Share Posted October 5, 2009 Hi Only idea I can suggest is to have a 2nd table with a single column which is an autonumber (starting at your start point), and when you want a new number you insert into that and then grab the inserted key value for use in your main table. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/176543-adding-auto-numbers/#findComment-930624 Share on other sites More sharing options...
daneth1712 Posted October 5, 2009 Author Share Posted October 5, 2009 Hi, thanks for your response, is there no way to do this via some kind of generator? I thought php had this function? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/176543-adding-auto-numbers/#findComment-930659 Share on other sites More sharing options...
Mchl Posted October 5, 2009 Share Posted October 5, 2009 In MySQL 5+ you can use ON INSERT trigger for that Quote Link to comment https://forums.phpfreaks.com/topic/176543-adding-auto-numbers/#findComment-930661 Share on other sites More sharing options...
daneth1712 Posted October 5, 2009 Author Share Posted October 5, 2009 Hi Mchl I am using MySQL version 5.1.36. Could you kindly explain to me how to do this? sorry for my noobishness with this. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/176543-adding-auto-numbers/#findComment-930668 Share on other sites More sharing options...
Mchl Posted October 5, 2009 Share Posted October 5, 2009 First off familiarise yourself with the manual documentation for triggers. Check out the examples provided and see if you can try to write it yourself. http://dev.mysql.com/doc/refman/5.1/en/triggers.html Quote Link to comment https://forums.phpfreaks.com/topic/176543-adding-auto-numbers/#findComment-930689 Share on other sites More sharing options...
Mr_J Posted October 5, 2009 Share Posted October 5, 2009 Hi, I`m no guru but wont rand or srand work? I`m sure you can generate numbers in sequence in PHP and simply store the number into its own field/column Quote Link to comment https://forums.phpfreaks.com/topic/176543-adding-auto-numbers/#findComment-930733 Share on other sites More sharing options...
Psycho Posted October 5, 2009 Share Posted October 5, 2009 What's wrong with using a standard auto-increment INT field and start it at whatever value you want. It's true that it won't be padded to 8 characters, but that is a display issue. By keeping the field as an INT type you get much more flexibility; e.g. select where field > [some_value]. Then whenever you want to display the field simply use string_pad() or some other methos to display with it padded to eight characters Quote Link to comment https://forums.phpfreaks.com/topic/176543-adding-auto-numbers/#findComment-930776 Share on other sites More sharing options...
Mchl Posted October 5, 2009 Share Posted October 5, 2009 What's wrong with using a standard auto-increment INT field and start it at whatever value you want. The problem is I already have an auto increment field in the database, and am not sure how to do this. Quote Link to comment https://forums.phpfreaks.com/topic/176543-adding-auto-numbers/#findComment-930782 Share on other sites More sharing options...
daneth1712 Posted October 5, 2009 Author Share Posted October 5, 2009 Hi guys, Thanks for your help, but I managed to do it another way. I added the code below in case anyone is interested.... just have to add script to insert next number into new record, which is the easy bit. <?php $d="10085201"; $db = mysql_connect("$host", "$user", "$pass") or die ("Error connecting to database."); mysql_select_db("$database", $db) or die ("Couldn't select the database."); $sql = "select id from $users ORDER by id asc"; $result=mysql_query($sql,$db) or die(mysql_error()); while($row=mysql_fetch_array($result)) { $reg=$row['id']; $sql2= "update $users SET d_id = '$d' WHERE id='$reg'"; $d ++; $result2=mysql_query($sql2 ,$db) or die(mysql_error()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/176543-adding-auto-numbers/#findComment-930807 Share on other sites More sharing options...
kickstart Posted October 5, 2009 Share Posted October 5, 2009 Hi Problem with this solution is when you come to add new rows in the future. If 2 scripts run simultaneously then the value assigned might not be what you expect. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/176543-adding-auto-numbers/#findComment-930815 Share on other sites More sharing options...
daneth1712 Posted October 5, 2009 Author Share Posted October 5, 2009 Hi Kevin, This script was only meant to be used once, all I needed to do was add a consecutive number to each record on the database. now I have done that, the script above is going to be binned. I will now use another bit of code to make sure the last record is checked and next number is added to new record. Thanks for everyones help! Quote Link to comment https://forums.phpfreaks.com/topic/176543-adding-auto-numbers/#findComment-930821 Share on other sites More sharing options...
Psycho Posted October 5, 2009 Share Posted October 5, 2009 What's wrong with using a standard auto-increment INT field and start it at whatever value you want. The problem is I already have an auto increment field in the database, and am not sure how to do this. OK, so use THAT field for this number. Just start the auto-increment from the number you need to start at and, as I stated above, use PHP to format the number appropriately for display purposes. Maybe I am missing something, but it would seem that two auto-increment fields for a table are superfluous. Quote Link to comment https://forums.phpfreaks.com/topic/176543-adding-auto-numbers/#findComment-930943 Share on other sites More sharing options...
Mchl Posted October 5, 2009 Share Posted October 5, 2009 It probably is used as a foreign key in several other tables. Changing its values would be tedious. It doesn't have to be continuous as some rows might have been removed in the past etc... Quote Link to comment https://forums.phpfreaks.com/topic/176543-adding-auto-numbers/#findComment-931039 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.