+ Reply to Thread
Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By farscapeone
  • 1 Post By dragonvirus23

Thread: [PHP+MySQL] How to get the next auto-increment value

  1. #1
    farscapeone's Avatar
    farscapeone is offline Community Advocate farscapeone is on a distinguished road
    Join Date
    Dec 2008
    Location
    Србија (Serbia)
    Posts
    1,166

    [PHP+MySQL] How to get the next auto-increment value

    If you ever wondered how to get the next auto increment value form MySQL table then this small tutorial is for you:

    PHP Code:
    // You have to connect to MySQL and select a database before you can do this
    $table_name "myTable";
    $query mysql_query("SHOW TABLE STATUS WHERE name='$table_name'");
    $row mysql_fetch_array($query);
    $next_inc_value $row["AUTO_INCREMENT"]; 
    You can find more info about "SHOW TABLE STATUS" MySQL command at:
    http://jayapal.instablogs.com/entry/...status-syntax/
    Last edited by farscapeone; 02-05-2010 at 07:04 AM.
    dinomirt96 likes this.

  2. #2
    ChatIndia's Avatar
    ChatIndia is offline Community Advocate ChatIndia is an unknown quantity at this point
    Join Date
    Sep 2009
    Location
    Exam Time. I won't tell you
    Posts
    1,296

    Re: [PHP+MySQL] How to get the next auto-increment value

    Thanks for this tutorial.
    Your appreciation is extremely Important. Click on the button at the bottom left corner of my post.

  3. #3
    scc123 is offline x10Hosting Member scc123 is an unknown quantity at this point
    Join Date
    Feb 2009
    Posts
    18

    Re: [PHP+MySQL] How to get the next auto-increment value

    Eh thats handy, all this time I've been using:
    PHP Code:
    $get=mysql_query("SELECT * FROM table");
    $got=mysql_fetch_array($get);
    $curr_id=$got['ID'];
    $next_id=$curr_id+1
    Which takes an extra line 0.o

  4. #4
    farscapeone's Avatar
    farscapeone is offline Community Advocate farscapeone is on a distinguished road
    Join Date
    Dec 2008
    Location
    Србија (Serbia)
    Posts
    1,166

    Re: [PHP+MySQL] How to get the next auto-increment value

    The code you are using is not exactly getting the next auto-increment value. It's just getting the biggest ID and increases it by 1. You can do that even more simple like this:

    PHP Code:
    $get=mysql_query("SELECT MAX(id) FROM table");
    $got mysql_fetch_array($get);
    $next_id $got['MAX(id)'] + 1
    But that won't work. Auto-increment is assigning a unique value every time, even if you deleted some rows. The point is, if you delete the highest value, next time you add something it will take that value, and that's not unique is it? For example, you have an id values like this:

    1
    2
    3

    You then delete 3 so you get

    1
    2

    Now if you use your code you'll get 3 as the next id value and you already had that value before. So it's not unique.
    If you use my code you'll get 4 cos that's the next unique value that MySQL uses for it's auto-increment option. Four is unique because you NEVER had that id value before in your table.
    Last edited by farscapeone; 02-15-2010 at 03:54 PM.

  5. #5
    scc123 is offline x10Hosting Member scc123 is an unknown quantity at this point
    Join Date
    Feb 2009
    Posts
    18

    Re: [PHP+MySQL] How to get the next auto-increment value

    Eh true but thats what I used =/
    Atleast now I have something that works now :D
    Thanks.

  6. #6
    dragonvirus23 is offline x10Hosting Member dragonvirus23 is an unknown quantity at this point
    Join Date
    Jan 2010
    Posts
    2

    Thumbs up Re: [PHP+MySQL] How to get the next auto-increment value

    thank you so much
    karimirt47 likes this.

  7. #7
    horion is offline x10Hosting Member horion is an unknown quantity at this point
    Join Date
    May 2009
    Posts
    22

    Re: [PHP+MySQL] How to get the next auto-increment value

    gracias

+ Reply to Thread

Similar Threads

  1. Grand Theft Auto: Liberty City Stories - PSP
    By dekarvn in forum Gamer's Lounge
    Replies: 10
    Last Post: 10-30-2005, 08:54 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers