visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: Unique numbers


By: krism U.S.A.  Date: 19/09/2004 00:00:00  English  Points: 20 Status: Answered
Quality : Excellent
Hi everyone


I have created an online booking form for a sports hall, the problem that I am having is each booking must have a unique number between 000 and 999. I have no idea how i can get the next availbe number from the database is there is one. If any one can help it would be much apreciated.

Thanks in advance
By: VGR Date: 19/09/2004 23:19:00 English  Type : Answer
SELECT MAX(id) FROM tablename;

I suggest having id integer unique auto_increment,althought this will not auto-limit it to 999
By: generalk Date: 20/09/2004 00:18:00 English  Type : Comment
Since you have a limited range of numbers, you might want to keep a special table of available numbers. In mySql it would look something like this. Other databases will be similar:

CREATE TABLE available_numbers (
free_number INTEGER NOT NULL
);

Whenever a new number comes available (somebody logs out, a bookig in cancelled, etc) do this:

INSERT available_numbers VALUES ($freeed_number)

then, to net the next available number, do this:

SELECT (@x:=free_number) AS next FROM available_numbers LIMIT 1;

and make sure to take it out of the table, like this:

DELETE FROM available_numbers WHERE free_number = @x;
By: T-Dob Date: 20/09/2004 00:58:00 English  Type : Comment
If the purpose is to add one more record in your database, you should better use the AUTO_INCREMENT feature of MySQL. Then, if to insert a new element, your specify all needed field except the id that will be automatically set to the next one.

ref: <A HREF="http://www.mysql.com/doc/en/example-AUTO_INCREMENT.html">http://www.mysql.com/doc/en/example-AUTO_INCREMENT.html</A>
By: wise0ne Date: 21/09/2004 17:53:00 English  Type : Comment
CREATE TABLE book (
uid int(3) NOT NULL auto_increment,
title varchar(60) default NULL,
author varchar(60) default NULL,
UNIQUE KEY uid (uid)
) TYPE=MyISAM;


Inserting data 2 table
INSERT INTO `book` (`uid`, `title`, `author`) VALUES (NULL, 'ABC', 'DEF');

when u giv NULL to uid it will auto_increment the value so u will get unique number all the time. Guess this solved ur problem






Do register to be able to answer

EContact
browser fav
page generated in 297.300100 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page