#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    51
    Rep Power
    15
    I need to get the next auto increment number
    that will be assigned to the table.
    Is there ez query to do this.
    Thanks, Charlie...

  2. #2
  3. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4492
    I don't think you can get the next auto_increment number. When you do an insert, you can use

    $number = mysql_insert_id()

    to get the number of the last record inserted. Don't think you can just add one to this number either, because if a lot of people are accessing your page at once, it will throw the numbers off.

    Maybe explain the layout of your code and someone can suggest a solution...

    ---JH

    ------------------
    *************************************************************
    * The manual can probably answer 90% of your questions... *
    * *
    * PHP Manual. www.php.net/manual *
    * MySQL Manual: www.mysql.com/documentation/mysql/bychapter *
    *************************************************************
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    51
    Rep Power
    15
    Thanks for the reply, I combed my MySQL book
    last night and didn't come up with a way so I
    will try the $number = mysql_insert_id() after I insert the data. Getting the next
    auto_increment number would be a usefull
    feature dont you think.
    Charlie...
  6. #4
  7. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    No, not really.

    As John said the value cannot be guaranteed at any point since another script may be inserting a record right after you pull the highest value, so the number you have as the next id might be taken before you can do anything with it.

    Also, last_insert_id() will only return a value if you've just done an insert.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    51
    Rep Power
    15
    Well this is the best way I came up with doing this, looks like it should be 99%
    reliable to me.
    Charlie...

    CODE:
    -------------------------------------

    $query = "insert into TABLE (DATA1,DATA2) values ('$data1','$data2')";

    $new_id = mysql_query($query, $db);

    // I now write the $new_id to a file and insert to table //

    $file = fopen("$dir/index.html","w");

    fputs($file,"<HTML>n<? include ("bld_page.php?cid=".mysql_insert_id($new_id).""); ?>n</HTML>n");

    fclose($file);

  10. #6
  11. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4492
    What exactly are you trying to do here? You insert new data into the database, then create an index.html file that includes a file based on the next ID?

    Why not pass something like cid=new, then have the file create the new information...it depends what you're actually trying to accomplish.

    maybe explain yourself a little more and someone can suggest some better logic to accomplish your goal.. ??

    ---JH
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    51
    Rep Power
    15
    -------------------------------------
    "Why not pass something like cid=new"
    -------------------------------------

    Becouse I can't get the next auto_increment number "$new", Only the last one assigned.
    So I have to add data to table then I have
    the last auto_increment number which I want
    to write to the index.html file.
    The code I posted works just wanted to post
    it for people who might have the same problem
    in the future.

    If you have a better way let me know.

    Thanks, Charlie...


  14. #8
  15. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4492
    Didn't you want to pass insert_id + 1 to the file? Something like this...

    $query = "insert into TABLE (DATA1,DATA2) values ('$data1','$data2')";
    $result = mysql_query($query, $db);
    // I now write the $new_id to a file and insert to table //
    $file = fopen("$dir/index.html","w");
    fputs($file,"<HTML>n<? include ("bld_page.php?cid=".mysql_insert_id()+1.""); ?>n</HTML>n");
    fclose($file);

    The mysql_insert_id() doesn't need anything passed as long as you only have one connection open to a database. if you have connections open to multiple databases, then you must pass the LINK_ID that was returned when you established your connection with mysql_connect.

    Is there any chance of this script being run more than once at the same time? Or is this something that only you will run? What can happen is, you'll insert something, then create the file with insert_id+1. If someone else runs the script at the same time, then the row you just passed to the new file will be taken up by the other insert, probably unrelated to what you are doing, right?

    If you want to drop this subject, just let me know...i'm just trying to help and understand these things myself.

    ---JH
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    51
    Rep Power
    15
    There is no need for the +1 in my case. What
    I am doing here is creating a new directory and a index.html file to go in that directory and adding data to the MySQL file.
    But I need a reference to the index.html so that when the index.html is called it will know which ID to call to get any other data
    needed. The reason I wanted the to get the
    next ID first is so that if there was some error in making the new directory or index.html it would not add data to table.
    I don't want it to add data to table until
    I am sure that the directory and it's file were created. And I will be the only person
    adding this data to the database. So I dont have to worry about getting the ID wrong.
    Charlie...

Similar Threads

  1. Replies: 6
    Last Post: March 18th, 2004, 06:58 PM
  2. Reset auto increment id's
    By slam1970 in forum MySQL Help
    Replies: 3
    Last Post: January 23rd, 2004, 04:11 PM
  3. delimited file import with auto increment
    By stressmonster in forum MySQL Help
    Replies: 2
    Last Post: October 8th, 2003, 12:21 PM
  4. prefixing an auto increment field
    By hade in forum MySQL Help
    Replies: 4
    Last Post: November 26th, 2001, 08:56 AM
  5. Setting auto increment
    By cube96 in forum MySQL Help
    Replies: 3
    Last Post: March 18th, 2001, 08:45 AM

IMN logo majestic logo threadwatch logo seochat tools logo