dlebowski Posted July 8, 2007 Share Posted July 8, 2007 This may actually be a question for the MYSQL forum, but I will give it a shot here first. I have a php/mysql page site that I am putting together and what I need is a mysql query that will query the database, select a specific table, a specific column, and output the next available number in sequence that has not been assigned yet. For example, this database is a customer database with each customer having their own ID. Every time I add a new customer, I have to know what ID is available. I would like the DB query to tell me what the next available number is. Any help on this would be great. Thanks in advance. -Ryan Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/ Share on other sites More sharing options...
AndyB Posted July 8, 2007 Share Posted July 8, 2007 If the customer_id was an auto-increment field, MySQL would automatically increment it when a new record was added and the value would be unique. Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-292366 Share on other sites More sharing options...
dlebowski Posted July 8, 2007 Author Share Posted July 8, 2007 In this particular case, I need to define a number for them. I have another field I created within my table that auto increments that is the primary key. This field is one I can just manually put a number into. Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-292367 Share on other sites More sharing options...
AndyB Posted July 8, 2007 Share Posted July 8, 2007 http://www.tizag.com/mysqlTutorial/mysqlmax.php Sounds like SELECT max and add one to it for the next customer number Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-292379 Share on other sites More sharing options...
dlebowski Posted July 9, 2007 Author Share Posted July 9, 2007 Thanks for the link. This will work with one exception. If I have 1, 2, 3, 4, 5, 7, 8, 9, 10 all used as customer numbers. The number 6 is an available number, but with "SELECT max" I will get the number 11. Is there any kind of query that may recognize that 6 is available? If not, I will go with the "SELECT max" suggestion provided to me as it will do the trick. Ryan Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-292963 Share on other sites More sharing options...
AndyB Posted July 9, 2007 Share Posted July 9, 2007 Life is simpler if you ignore 'gaps' (like 6 being available) and use MAX + 1 for the next number. Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-292964 Share on other sites More sharing options...
dlebowski Posted July 9, 2007 Author Share Posted July 9, 2007 I completely agree, but if it's possible it would be better for my client. They are an auction company and some customers like having 1 digit numbers and other want four digits numbers. The first person that wants an ID in the 9000's, will result in the next available number as 10,000 before too long. But, I understand that there may be not other choice. Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-292968 Share on other sites More sharing options...
zfred09 Posted July 9, 2007 Share Posted July 9, 2007 Couldn't you just check each id until you find one that is available? Like have a field named available with 1 being available and 0 being taken, then a simple query like SELECT customer_id FROM db WHERE available = '1'; //didn't check syntax for the available slots would return the number available. Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-292969 Share on other sites More sharing options...
AndyB Posted July 9, 2007 Share Posted July 9, 2007 Couldn't you just check each id until you find one that is available? Rather impractical in a table with 10,000 records and 9999 is available. 9999 mysql queries instead of one or retrieving the entire table and stepping through a 10,000 element array to get 10000+1:) Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-292974 Share on other sites More sharing options...
zfred09 Posted July 9, 2007 Share Posted July 9, 2007 Very true. Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-292977 Share on other sites More sharing options...
dlebowski Posted July 9, 2007 Author Share Posted July 9, 2007 So am I S.O.L on this one? Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-292982 Share on other sites More sharing options...
AndyB Posted July 9, 2007 Share Posted July 9, 2007 No. There's the easy way - already described - and there's the hard way. And the hard way is going to involve a load of coding and even more database-bashing or processing that'll slow down your site. In the end, it's up to you. Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-292985 Share on other sites More sharing options...
sasa Posted July 9, 2007 Share Posted July 9, 2007 try <?php mysql_connect('localhost'); mysql_select_db('test'); $res = mysql_query('select customer_id FROM customer ORDER BY customer_id' ); $j = mysql_num_rows($res); $i = 0; while ($i++ < $j and mysql_result($res, $i - 1, 0) == $i); echo 'Next available number is: ', $i; ?> Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-292991 Share on other sites More sharing options...
dlebowski Posted July 9, 2007 Author Share Posted July 9, 2007 This query is just giving me the first ID in my list. In this case it's 1. Let me do some more testing. Thanks for giving it a shot. Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-292996 Share on other sites More sharing options...
cooldude832 Posted July 9, 2007 Share Posted July 9, 2007 why does their ID matter if you want uniformness make it a 6 digit number like 000001 000011 001111 010092 etc etc Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-292999 Share on other sites More sharing options...
dlebowski Posted July 9, 2007 Author Share Posted July 9, 2007 Sasa, This actually works on my primary key field, but not the ID?? Not sure why that would be, but it does seem to work on the primary key field. Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-293002 Share on other sites More sharing options...
sasa Posted July 9, 2007 Share Posted July 9, 2007 what is name of your field? Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-293007 Share on other sites More sharing options...
dlebowski Posted July 9, 2007 Author Share Posted July 9, 2007 THe problem is that in the ID field was the value "0". It didn't like that. Once I deleted it, it appears to work! I will try and figure out what I need to add to that code you provided to work with a field of 0 in it. Thanks again!!! Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-293009 Share on other sites More sharing options...
sasa Posted July 9, 2007 Share Posted July 9, 2007 try <?php mysql_connect('localhost'); mysql_select_db('test'); $res = mysql_query('select customer_id FROM customer ORDER BY customer_id' ); $j = mysql_num_rows($res); $i = 0; while ($i < $j and mysql_result($res, $i, 0) == $i) $i++; echo 'Next available number is: ', $i; ?> Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-293019 Share on other sites More sharing options...
dlebowski Posted July 9, 2007 Author Share Posted July 9, 2007 "Warning: mysql_result() [function.mysql-result]: Unable to jump to row -1 on MySQL result index 3 in /test.php on line 11" is the error I get. I may just not be able to have an ID of zero in there. If that is the case, I can deal with that. Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-293021 Share on other sites More sharing options...
sasa Posted July 9, 2007 Share Posted July 9, 2007 "Warning: mysql_result() [function.mysql-result]: Unable to jump to row -1 on MySQL result index 3 in /test.php on line 11" is the error I get. I may just not be able to have an ID of zero in there. If that is the case, I can deal with that. $i always >=0 Warning is impossible Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-293486 Share on other sites More sharing options...
dlebowski Posted July 9, 2007 Author Share Posted July 9, 2007 Sasa, I just modifed my code to prevent the entry of "0" as an ID. This is going to work fine. Thank you very much! Spent many hours on this one. -Ryan Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-293488 Share on other sites More sharing options...
chocopi Posted July 9, 2007 Share Posted July 9, 2007 EDIT: Nevermind, its been sorted Quote Link to comment https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/#findComment-293498 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.