Languages :: PHP :: Unique numbers |
|||
| By: krism |
Date: 19/09/2004 00:00:00 |
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 | 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 | 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 | 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 | 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 |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








