Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/58911-solved-next-available-number-in-seqence/
Share on other sites

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

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.

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.

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:)

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;
?>

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!!!

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;
?>

 

"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.

 

 

"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 
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.