0

How can I get the highest number in my ID column?

With MySQL I have the connection part done I just need to know how I can display that number using PHP. I got some of my code from a friend:

    <?php
    $con = mysql_connect("quollcraft.net", "quollcr1_forum", "pw");
    if (!$con) {
    die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("quollcr1_hub", $con);

    $result = mysql_query("SELECT * FROM recentplayers ORDER BY id DESC LIMIT 28");

    echo "<table>";
    while ($row = mysql_fetch_array($result)) 
        echo "<td>";
        echo "<center>";
        echo '<img class="avatar" src="http://cravatar.eu/avatar/' . $row ['name'] .         '/40.png" rel="tooltip" style="display: inline-block" title="' . $row ['name'] . '">';

        echo "</td>";
    echo "</table>";    




    mysql_close($con);
    ?>
    <?php
    $con = mysql_connect("quollcraft.net", "quollcr1_forum", "pw");
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("quollcr1_hub", $con);

    $result = mysql_query("SELECT * FROM id ORDER BY id DESC LIMIT 1");
    echo "<table>";
    while ($row = mysql_fetch_array($result)) 
        echo "<td>";
        echo "<center>";

        echo "<p>' . $row ['id'] . 'hello</p>";
        echo "</td>";
    echo "</table>";    




    mysql_close($con);
    ?>`

Its supposed to show my latest players then the total players i have.

  • Even you already get tons of trivial answers (as the question might appear trivial), may I ask back why you need that MAX(ID) value? I assume you're looking for the current auto increment value, linking the dupe then. – hakre May 04 '14 at 06:01

5 Answers5

1

If you want the full row with the highest id not only the id, this would give you that

SELECT * FROM table ORDER BY id DESC LIMIT 1

If you want the id as the only value you can do

SELECT MAX(id) as id FROM table 

If you want the total amount of rows in the table, to figure out how many players you have you can do

SELECT COUNT(id) as total_players FROM table 
Cleric
  • 3,167
  • 3
  • 23
  • 24
1

You can use the aggregate function MAX() to find the maximum value in a column:

SELECT MAX(ID) as ID
FROM TableName
Raging Bull
  • 18,593
  • 13
  • 50
  • 55
0
SELECT MAX(YOUR COLUMN) FROM YOUR TABLE 
underscore
  • 6,495
  • 6
  • 39
  • 78
0

MySQL query should look like this

SELECT id FROM table ORDER BY id DESC LIMIT 1 

The solution with MAX may not be the best one when you have multiple records in database.

I don't know how you connect to your MySQL in PHP so it depends how you will display those max id in PHP

Ascherer
  • 8,223
  • 3
  • 42
  • 60
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
0

You can also use this:

SELECT id FROM YOUR_TABLE WHERE id = LAST_INSERT_ID();
Ansh
  • 94
  • 4