How to get the next ID to be inserted?

Asked

Viewed 1,336 times

0

I’d like to get the next ID inserted, something like:

$ultimoid = (DB::lastInsertId());
$futuroid = ( $ultimoid + 1 );

The goal is to previously know the ID that will be used in my INSERT because before entering the record it is necessary to save a file in a folder whose name references the ID of this record.

  • 1

    The lastInsertId is what has just been created. Isn’t that what you need? Why the +1 in the title? Please clarify the question.

  • is pq in case I would create the directory and uple the image before the ID is even created

  • PDO :: lastInsertId - Returns the ID of the last inserted row or sequence value as I will know whether it is the value of the last completed id or the sequence?

  • 4

    It’s the last one filled out. You can even know the next one (in mysql at least), but there is no way to guarantee that this next one will be in fact what you will gain at the time of insertion (because there may be more than one user accessing at the same time, among other factors). I recommend filling in the line and getting the last id inserted, even if you need to update the record afterwards.

  • then in that case it would even have to add +1 to create the same folder, since the action of creating and inserting would be at the same instant it would be almost impossible to create the folder other than the correct id .. Tks by the doubt taken

  • 2

    I think you are misunderstanding the use of lastInsertId. It will return the id generated by the immediately previous query.

  • It’s kind of confusing, to be clear to me assuming that in my news table the last id is 71. if I call $ultimoid = (DB::lastInsertId(); it will return me 71 or 72?

  • 1

    It will return 71. But if I’m not mistaken you can either return anything or give error if you don’t run right after the Insert.

  • 2

    @Arsomnolasco why try to guess the next ID ? Can’t upload the image after Insert? (Believe: the almost impossible of simultaneous users happens)

  • 1

    Why is this question -5 and is being voted on to close?

  • 1

    This question is being discussed at the goal: http://meta.pt.stackoverflow.com/questions/2377/como-peg-o-pr%C3%B3ximo-id-a-ser-insert

Show 6 more comments

1 answer

4


This is a fairly common need and in other databases is solved with SEQUENCE.

Sequence is different from auto increment because using Quence you (or the framework or database through a Trigger) first gets the next ID, reserving this ID, and then, even though much later and even in another transaction, the ID can be used quietly because it will be unique - the quence will never again hand over that same ID (unless it is reset).

Unfortunately Mysql does not have this feature yet, it only has the auto increment simple so that you cannot book an ID.

You could create a function that behaves like a quence true, but it is quite laborious because you have to ensure for example the control of competition and still have to manually create a Rigger for each table where you want to use this resources.

Here’s an example of how the resource auto increment is implemented through the use of quence in other databases: Firebird SQL auto-increment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.