473,396 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

GET Auto_Increment Number BEFORE Insertion PHP MYSQL

dlite922
1,584 Expert 1GB
I'm builing an order management system. They're old system that i'm replacing gives them the NEXT order number when they open a new order.

(ie QuickBooks acts this way)

How can i do the same thing with PHP MySQL. I'm thinking about this SQL query

SELECT num FROM table ORDER BY num DESC LIMIT 1;

that should give me the last number and i can just add one to it.

BUT i do not want to query the DB as this will contains thousands of records and i'm worried about a performance hit.

Is there any other way to achieve this? has anyone encountered this issue before?

I want the user to enter any order number, but automatically suggest the next available one. This way they can "choose" a scheme. If somebody types in O1000, the next sales person should see O1001 when entering a new order.

thanks for your help guys,


DM
Mar 19 '08 #1
15 3564
satas
82
In my opinion following query should not takes a lot of database resources:
Expand|Select|Wrap|Line Numbers
  1. SELECT MAX(num) FROM table
And it is not a good idea to made users thinking about something they should not worry about.
Mar 19 '08 #2
dlite922
1,584 Expert 1GB
In my opinion following query should not takes a lot of database resources:
Expand|Select|Wrap|Line Numbers
  1. SELECT MAX(num) FROM table
And it is not a good idea to made users thinking about something they should not worry about.
I Don't know what you mean by that last statement, but you gave me another query for a solution to think about. I don't understand how my user is worring about this? Their need is to know the order number upon entry, not after.

Maybe i'll just have a counter table.

[ Counter ]
> ID INT
> TableName VARCHAR()
> Number INT

6 tables (order table being one of them), i'll add one each time for a new order is requested?

This way the SQL will query a 4 record long table instead of a 4000 record table.

Yet this is another SQL solution. Guess it has to go somewhere, DB is as good of a place as any.
Mar 19 '08 #3
satas
82
Their need is to know the order number upon entry, not after.
I don't understand why you want users to enter order numbers.
Why do they need to enter it while it can be calculated by the one sql query?

Maybe i'll just have a counter table.

[ Counter ]
> ID INT
> TableName VARCHAR()
> Number INT

6 tables (order table being one of them), i'll add one each time for a new order is requested?

This way the SQL will query a 4 record long table instead of a 4000 record table.

Yet this is another SQL solution. Guess it has to go somewhere, DB is as good of a place as any.
I've just tested a query from my pervious post on the table with 100 000+ records. It takes 0.0280 seconds.
Mar 19 '08 #4
ronverdonk
4,258 Expert 4TB
In your thread title you indicated that you were talking about and auto_Increment number. From the posts I don't see that auto_increment coming into the discussion.

Or have you changed the question by meaning that you do not use an auto_increment number but a user-increment number?

I lost the discussion on the thread title.

Ronald
Mar 19 '08 #5
TheServant
1,168 Expert 1GB
Still a little confused, but if you are giving your customer an order number before it is in MySQL and you're using MySQL to select the next number: What happens when there are two people on at the same time. The first will get say number 144, and so will the second? Will that be a problem?

If so, I recommend you find another way to do your order numbers, maybe register a number in MySQL to commence the form (or whatever), and then update the table when they have entered their details.
Mar 19 '08 #6
Atli
5,058 Expert 4TB
Hi.

One solution would be to create a totally separate table, containing a single field that you update every time a new record is in the making...

That is, you could create a table with a single integer, which contains the highest yet to be created index.

Every time the form for a new order is shown, you grab this number and increment it by one. That way, even if there are 10 different people creating a new order at the same time, each of them will have a different number.
Mar 19 '08 #7
TheServant
1,168 Expert 1GB
^^ Articulated a little better than my post but that's what I was getting at ;)
Mar 20 '08 #8
mcfly1204
233 100+
We use SalesLogix at our company and they implement this same method. They have a separate table that generates a random number for ID's, what your seeking is no different.
Mar 20 '08 #9
Markus
6,050 Expert 4TB
Hey guys,

Take a look at this thread!

Regards.
Mar 20 '08 #10
ronverdonk
4,258 Expert 4TB
Hey guys,

Take a look at this thread!

Regards.
What happens when there are two people on at the same time. The first will get say number 144, and so will the second?
marcusn00b's solution, using the Schema table of tables, does not take away the problem that The Servant addresses.

The problem is still that both 2 users get their info from the MySQL Schema table of tables. There is no 'ENQUEUE' mechanism that allows you to reserve a number.

Ronald
Mar 20 '08 #11
dlite922
1,584 Expert 1GB
Hi.

One solution would be to create a totally separate table, containing a single field that you update every time a new record is in the making...

That is, you could create a table with a single integer, which contains the highest yet to be created index.

Every time the form for a new order is shown, you grab this number and increment it by one. That way, even if there are 10 different people creating a new order at the same time, each of them will have a different number.

and i quote myself:


Maybe i'll just have a counter table.

[ Counter ]
> ID INT
> TableName VARCHAR()
> Number INT
Rover, Maybe my title is not the best, but it is auto increment.

My user does not HAVE TO enter an order number, he'd like to KNOW it upon creating an order (filling it out)

HOWEVER, there are instances where they'd like to ENTER ONE (say in future)

This will conflict, so this requirement is now dropped, they cannot pick "future unused" order numbers because this will run into problem with my counter table.

Counter table is best. I don't care about lost numbers, ie if someone request a new order (say 4050) and doesn't end up using it (cancels) that number is gone forever and that's fine.

With the counter table, multiple people can request new orders and it won't run into conflict (with the "get next number in table", conflict does occure)

Thanks for all those who helped:, unless anyone has any better the best solution now is:

Create a counter table that keeps track of order number, invoice number, and any number needed.

thanks again.
Mar 21 '08 #12
dlite922
1,584 Expert 1GB
Hey guys,

Take a look at this thread!

Regards.
I think i saw that, but didn't find it again, (mostly because i wasn't search for auto_increment keyword, but some how had a loss for words in my title and that came to mind)

Never the less, that doesn't solve my problem (as it became obvious in the conversations in this forum)
Mar 21 '08 #13
dlite922
1,584 Expert 1GB
We use SalesLogix at our company and they implement this same method. They have a separate table that generates a random number for ID's, what your seeking is no different.
I've worked with sales logic and DACEasy, both implement this, but had not seen this discussed in a web app.

That's exactly what i'm looking for, was wondering an even FASTER solution is possible that we can't think of besides creating a "counter" table.
Mar 21 '08 #14
mysql_query(insert_id);
it can help to take autoincrement insert id after insert query
Mar 21 '08 #15
Markus
6,050 Expert 4TB
I think i saw that, but didn't find it again, (mostly because i wasn't search for auto_increment keyword, but some how had a loss for words in my title and that came to mind)

Never the less, that doesn't solve my problem (as it became obvious in the conversations in this forum)
Sorry!
I hadn't kept track of the conversation.

My apologies.
Mar 21 '08 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Paul Lamonby | last post by:
Hi, i want to create a unique serial number to my Db entries. I thought the best way would be to add the auto_increment primary key value to a string, then insert it into a table field...
4
by: Pjotr Wedersteers | last post by:
I have a table with quiz questions. Each question has a unique ID, based on an auto_increment field. Now I've discovered that with deleting rows from the table the deleted ID's are not reissued....
0
by: Miguel Perez | last post by:
Hi everyone: Does anyone know how to reset the auto_increment value of certain table. Any ideas or sugestions Greetings in advance ...
2
by: Ittay Freiman | last post by:
i cannot set auto_increment to start from anything other than 1: mysql> create table test (id int unsigned not null auto_increment primary key); mysql> alter table test auto_increment=2; ...
0
by: Write a Friend | last post by:
When using AUTO_INCREMENT, is there a way to set the starting value. Thanks, Carlos -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: ...
4
by: jjliu | last post by:
i have created the following table: create table test (field1 varchar(100), id integer unsigned not null auto_increment, unique (field1), primary key (id)); When i try to insert some values by...
9
by: Bart Van der Donck | last post by:
Hello, The first column of my table is AUTO_INCREMENT. I fill my table with 5 records with a blanco value in their first field. The first column of my table will then hold the values 1,2,3,4,5...
0
by: Shailesh | last post by:
I made a MyISAM table on mysql 4.0.18 NT with auto_increment column started at 2147483646. The third row I insert fails as expected because the integer range is maxed out. However, if I delete...
1
by: bcm | last post by:
I am migrating a database to MySQL 5.0. Several tables should have their primary key as an auto_increment integer. The database being copied has records with the primary key beginning with zero...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.