Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Banned
    Join Date
    Jan 2012
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Finding out the highest number in an auto-incrementing table

    I have a row in a MySQL table called id, which auto-increments.

    I was wondering if anyone knows a PHP solution to find out what the highest number in the row is?

    I am thinking perhaps execute a MySQL query that will list the highest number, and then echo the result, like $row[id] in PHP?
    Last edited by FlashDancer; 01-21-2012 at 10:32 AM.

  • #2
    Regular Coder
    Join Date
    Jun 2010
    Posts
    278
    Thanks
    63
    Thanked 8 Times in 8 Posts
    PHP Code:
    /*
     * Define $server, $username, $password, $database and $table here as follows:
     *  $server = name of mysql server.
     *  $username = username for mysql account.
     *  $password = password for mysql account.
     *  $database = name of database to use.
     *  $table = name of table containing the id column.
     */

    $conn mysql_connect($server$username$password);
    mysql_query("use $database;"):
    $res mysql_query("select max(id) from $table;");
    $data mysql_fetch_array($res);
    $count $data[0];
    mysql_close($conn);

    echo 
    $count.PHP_ENDL
    Of course, you'll need to put some error checking in, but what I've posted above is the basic code to do what you want.
    Last edited by XmisterIS; 01-21-2012 at 12:10 PM.

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    Let me ask, what's your ultimate goal? It looks like you are after a wrong path.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,091
    Thanks
    51
    Thanked 506 Times in 493 Posts
    You can also use:

    select id from <table> order by id desc limit 1
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Do you want the highest number there or do you want the one your script just inserted?

    If the latter then you want "SELECT LAST_INSERT_ID()"

    That function retrieves the last value actually inserted by the currently running copy of the script and will ignore any higher numbers inserted by someone else since you inserted yours.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •