Finding the next number

Discussion in 'PHP' started by adamjblakey, Oct 18, 2008.

  1. #1
    Hi,

    When i add a new entry into my table i want to check all previous rows in the field row_order to check what is the highest numbers. Then i want to add 1 to this number and use it for the current entry in the row_order field.

    How would i do this?

    Cheers,
    Adam
     
    adamjblakey, Oct 18, 2008 IP
  2. Grit.

    Grit. Well-Known Member

    Messages:
    1,424
    Likes Received:
    22
    Best Answers:
    1
    Trophy Points:
    110
    #2
    why not just have a column that has the auto-increment on it? This makes the new row with the next highest number
     
    Grit., Oct 18, 2008 IP
  3. Krnl

    Krnl Peon

    Messages:
    60
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Why don't you just set the row_order field to auto_increment?
     
    Krnl, Oct 18, 2008 IP
  4. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Its because the column values will be changed by the user.
     
    adamjblakey, Oct 18, 2008 IP
  5. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    make row_order auto_increment

    edit: if for whatever reason you don't want to do that, you could always get the highest number like this:

    SELECT row_order FROM table ORDER BY row_order DESC LIMIT 1
     
    Kyosys, Oct 18, 2008 IP
  6. Krnl

    Krnl Peon

    Messages:
    60
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #6
    There may be a short-cut that I don't know of, but I guess you'll need to query the database to get the value that you're looking for (select row_order from tablename order by row_order desc limit 1), save that off and re-query the db to insert the new row, adding 1 to the result of the previous query for row_order.

    Is this what you're trying to do, or am I missing something?
     
    Krnl, Oct 18, 2008 IP
  7. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #7
    Right it is working now thanks :)
     
    adamjblakey, Oct 18, 2008 IP
  8. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Why are you doing it like this, though? That seems pretty... inefficient
     
    Kyosys, Oct 18, 2008 IP
  9. AdnanAhsan

    AdnanAhsan Well-Known Member

    Messages:
    601
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #9
    why are you making confussion brothers. just use MAX(id)

    like

    $max = max id
    $next = $max + 1

    max id should be taken from db using MAX(id) ... thanks
     
    AdnanAhsan, Oct 18, 2008 IP