Get Maximum Value of Column with PHP MYSQL

Get Maximum Value of Column with MYSQL with MAX() and ORDER BY

With mysql, you can use the MAX() function to get the highest value in a column. The column could have data like ages, auto incrementing ids or weights. If you want just the highest value, the max() function will output that value.

If you want a list of values starting with the highest number, you use the 'ORDER BY' condition in a mysql query to output the data based on what you want; such as highest value or most recent date. The example below explains how such data is sorted.

$command = "SELECT MAX(id) as maxid FROM table_sort";
$result = mysqli_query($db, $command);
while ($row = mysqli_fetch_assoc($result)){
$myid = $row['maxid'];
echo "<br/>";
$name = $row['firstname'];
echo "Here is the max id: ".$myid."<hr>";
}$command = "SELECT id, firstname FROM table_sort ORDER BY id desc";
$result = mysqli_query($db, $command);
while ($row = mysqli_fetch_assoc($result)){
$myid = $row['id'];
$name = $row['firstname'];
echo "My id is ".$myid." and first name is ".$name."<br/>";