Find Next Available Id

Discussion in 'PHP' started by adamjblakey, Aug 10, 2009.

  1. #1
    Hi,

    Is there a way to query the database to find the next available id number?

    Cheers,
    Adam
     
    adamjblakey, Aug 10, 2009 IP
  2. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #2
    If you are just trying to write a new item into it I think it automatically just adds it to the newest one otherwise I don't know of anyway to find it.
     
    Pudge1, Aug 10, 2009 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    No, its a long story but i just need the next available number.
     
    adamjblakey, Aug 10, 2009 IP
  4. arunn

    arunn Active Member

    Messages:
    347
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #4
    
    function get_insert_id($table)
    {
        $q = "SELECT LAST_INSERT_ID() FROM $table";
        return mysql_num_rows(mysql_query($q)) + 1;
    }
    
    
    Code (markup):
    hope this helps you
     
    arunn, Aug 10, 2009 IP
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    thank you very much :)
     
    adamjblakey, Aug 10, 2009 IP
  6. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #6
    mysql_num_rows()
     
    Pudge1, Aug 10, 2009 IP
  7. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #7
    I have tried that function but it does not seem to be bringing back the correct number? Is it correct?
     
    adamjblakey, Aug 10, 2009 IP
  8. arunn

    arunn Active Member

    Messages:
    347
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #8
    i use a function like this.. i take from the table index itself

    function next_id($table) {
    $sql = 'SHOW INDEX FROM '.$table;
    $result = mysql_query($sql);
    return $result[Cardinality];
    }
     
    arunn, Aug 10, 2009 IP
  9. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #9
    If you've just performed an insert query you could also do:

    $available = mysql_insert_id() + 1;
    PHP:
    assuming id is auto_incr and next available is +1
     
    wd_2k6, Aug 10, 2009 IP
  10. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #10
    No sorry i have not just performed an insert.
     
    adamjblakey, Aug 10, 2009 IP
  11. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #11
    If this is a publicly accessed page, this is probably not a good way to do this. If you have 2 concurrent visitors, and you are pre-seeding a value based on the next available, you're very likely to get crossed id's, or collisions.

    If you need the next ID, and need to guarantee it will be accurate when you use it, best bet is to make an insert into the table, then get the insert_id(), and then do an update when you actually want to put something in there.

    It is an extra step, but will prevent some potentially ugly problems resulting from id collisions.
     
    jestep, Aug 10, 2009 IP
  12. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #12
    Thanks for you concern but it will not have multiple visitors as this did cross my mind.
     
    adamjblakey, Aug 10, 2009 IP
  13. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #13
    last_insert_id gets the last insert from the current open mysql resource/connection. You can't get the insert_id without making an insert on the same mysql resource/connection.

    To get the id, do something like this...

    SELECT id FROM my_table ORDER BY id DESC LIMIT 1;

    You can then just add 1 to the value returned...
     
    jestep, Aug 10, 2009 IP
  14. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #14
    You should probably just setup your database so that the id field is set to be auto_increment, this way you can insert without including an Id and it'll automatically autoincrement to the next value. Would save you a heck of a hassle than trying to figure out what's the last ID then going from there.
     
    kblessinggr, Aug 10, 2009 IP