July 5, 2006

Find Next Autoindex Using PHP and MySQL

I’m putting on my programmer’s hat tonight to solve a mystery. I was searching Google tonight to try to find the way you would use PHP to grab the next AUTO_INCREMENT value out of a MySQL table. You know, if you’re getting ready to insert data into a table, and you want to know beforehand what the next autonumber is going to be on the ID field. I found the site that shows you how to do it.

$tablename = "tablename";
$next_increment = 0;
$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]";

Just jotting this down here as a note so I can remember for next time.

Filed under The Computer Vet Weblog

Comments (6)

Comments RSS - Write Comment

  1. pkc3980 says:

    Ironically enough, I was researching that this morning before I checked here. I ran across the same page as you did…

    You put on your programmers hat to solve problems? I normally put on my black white hat (if you catch my drift…)

    Posted July 6, 2006 @ 6:32 am
  2. beto barbosa says:

    Thanks this process work properly

    Posted January 12, 2007 @ 4:06 pm
  3. Joe Root says:

    Thanks alot, this helped me out a great deal.

    Posted February 17, 2007 @ 10:08 am
  4. Geraldo Protta says:

    Thx alot!
    FUNCIONA! E é muito simples de aplicar!

    Posted February 28, 2008 @ 9:18 pm
  5. tzywen says:

    Thanks a lot. Exactly what I’m looking for 😉

    Posted November 21, 2008 @ 11:34 pm
  6. Sergey says:


    But i use simple))

    $info=mysql_fetch_assoc(mysql_query(“SHOW TABLE STATUS LIKE ‘catalog_base'”)) or die(mysql_error());

    Posted July 9, 2010 @ 7:18 am

Write Comment