GET Auto_Increment Number BEFORE Insertion PHP MYSQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    GET Auto_Increment Number BEFORE Insertion PHP MYSQL

    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
  • satas
    New Member
    • Nov 2007
    • 82

    #2
    In my opinion following query should not takes a lot of database resources:
    Code:
    SELECT MAX(num) FROM table
    And it is not a good idea to made users thinking about something they should not worry about.

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      Originally posted by satas
      In my opinion following query should not takes a lot of database resources:
      Code:
      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.

      Comment

      • satas
        New Member
        • Nov 2007
        • 82

        #4
        Originally posted by dlite922
        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?

        Originally posted by dlite922
        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.

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          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

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            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.

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

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

              Comment

              • TheServant
                Recognized Expert Top Contributor
                • Feb 2008
                • 1168

                #8
                ^^ Articulated a little better than my post but that's what I was getting at ;)

                Comment

                • mcfly1204
                  New Member
                  • Jul 2007
                  • 233

                  #9
                  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.

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    Hey guys,

                    Take a look at this thread!

                    Regards.

                    Comment

                    • ronverdonk
                      Recognized Expert Specialist
                      • Jul 2006
                      • 4259

                      #11
                      Originally posted by markusn00b
                      Hey guys,

                      Take a look at this thread!

                      Regards.
                      Originally posted by TheServant
                      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

                      Comment

                      • dlite922
                        Recognized Expert Top Contributor
                        • Dec 2007
                        • 1586

                        #12
                        Originally posted by Atli
                        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:

                        Originally posted by dlite922

                        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.

                        Comment

                        • dlite922
                          Recognized Expert Top Contributor
                          • Dec 2007
                          • 1586

                          #13
                          Originally posted by markusn00b
                          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)

                          Comment

                          • dlite922
                            Recognized Expert Top Contributor
                            • Dec 2007
                            • 1586

                            #14
                            Originally posted by mcfly1204
                            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.

                            Comment

                            • mageswar005
                              New Member
                              • Mar 2008
                              • 72

                              #15
                              mysql_query(ins ert_id);
                              it can help to take autoincrement insert id after insert query

                              Comment

                              Working...