Jump to content

[SOLVED] AutoNumber


jkkenzie

Recommended Posts

Hi!

Say, i have in my mysql table a field named "id". It is an autonumber field and it is the primary key.

 

I woul like to know if it is possible to get the next autonumber which is going to be used to save a record.

 

I need to use the id field number to save something on a directory.

 

regards,

Joe

Link to comment
Share on other sites

Because of the possibility of multiple concurrent operations, you cannot retrieve the current highest number, add one, and actually use it. If you are at the point of needing to use the next auto increment number, insert the row in the database (even if you must insert some empty fields and go back to update them later), then get the actual id that was used in that insert operation.

Link to comment
Share on other sites

If you are getting this number after an INSERT method, you can do this:

 

mysql_query("INSERT INTO table_name (`col1`,`col2`) VALUES ('data1','data2')");
$cVal = mysql_insert_id();  // the insert number from above
$nVal = mysql_insert_id() + 1;  // the next insert number from above
echo '
Last insert id: '.$cVal.'<br>
Next insert id: '.$nVal;

Link to comment
Share on other sites

I WILL be the next insert id, doesn't matter if it has been inserted or not, it still will be after what you inserted, thus being the next insert.

 

No, it might not be.  You are assuming that you are the only person accessing and running the script.  If you have 10 users, 100, 1000, etc... all doing stuff on your website, you have no guarantee that one (or more) didn't do something that updated your db in that time. 

Link to comment
Share on other sites

Multi-tasking operating systems use interrupt driven time slicing to execute multiple tasks concurrently. Multiple concurrent requests for web pages that perform database queries cannot guarantee the order in which the database queries are executed. You cannot guarantee what an auto increment id is until after it has been assigned.

Link to comment
Share on other sites

mysql_insert_id() will show the last auto_inc'd id from your personal connection stream.  It is dependable in that aspect, yes.  But you can't just add 1 to that and assume that the next one is available, because it might not be.  Someone else could have gone to a webpage on your site that inserted another row in that time, and thus, the "next available" one would really be mysql_insert_id() +2.  Get it?

Link to comment
Share on other sites

mysql_insert_id() will show the last auto_inc'd id from your personal connection stream.  It is dependable in that aspect, yes.  But you can't just add 1 to that and assume that the next one is available, because it might not be.  Someone else could have gone to a webpage on your site that inserted another row in that time, and thus, the "next available" one would really be mysql_insert_id() +2.  Get it?

 

Completely under stand, and understood before my post.

Link to comment
Share on other sites

This may be as close as you can get:

 

<?php
include './incl/db.php'; // Include your database connection
$tablename = "users"; // Table name of get increment
$qShowStatus = "SHOW TABLE STATUS LIKE '$tablename'";
$qShowStatusResult = mysql_query($qShowStatus) or die ( "Query failed: " . mysql_error() .  $qShowStatus );

$row = mysql_fetch_assoc($qShowStatusResult);
$next_increment = $row['Auto_increment'];

echo "next increment number: [$next_increment]";
?>

Link to comment
Share on other sites

Please read the first post. He wants to use the number to "save something on a directory"

 

And he was already told the proper way to do this so that it will work under all circumstances without needing to create a lot of special case conditions that cannot be guaranteed to work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.