Generate Numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fullfocus
Forum Commoner
Posts: 33
Joined: Fri Jun 30, 2006 2:51 pm

Generate Numbers

Post by fullfocus »

Hello:

I hope someone can help me out.

I have a table which stores client information. In this table I also need to store a field called case_number.

My client would like the case_number to be automatically generated when she adds a new client. And, she would like the case numbers to be sequential.

So if I have ClientA in a table with case number 2020 and I add ClientB to the table, I would like their case number to be 2021. And, if I add ClientC to the table, their case number sould be 2022. Etc.

Is it possible to write a script to generate a number in sequential order and store that number into a table?

If so, how do I go about scripting something like this?

Thank you in advance.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post by smudge »

Two ways to do this: query the server for the last entered number, increment, and insert the new row; or check out the auto_increment option when making tables.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

smudge wrote: query the server for the last entered number, increment, and insert the new row;
What if in the time between the query for the highest number and the insertion another session inserts value?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Yes, use an Auto Increment field. The database engine will insure that every case number is unique. The numbers will be assigned consecutively, based on the exact sequence the records were inserted.
Post Reply