Talon Talon - 3 months ago 7
MySQL Question

Get the Largest Number in a mySQL Database in PHP

I have a SQL Database that has a column like this:

ID
-----
0352
5432
4382
3520
30593
3992
295


What I want to do is search through that column, find the largest number (30593) and store it in a variable.

This database has other columns, the example above is for demonstration only.

e.g.

$largestNumber = GET LARGEST NUMBER FROM ID

How would I do that in PHP / MySQL

Answer

In PHP, we do it like this:

$rowSQL = mysql_query( "SELECT MAX( ID ) AS max FROM `tableName`;" );
$row = mysql_fetch_array( $rowSQL );
$largestNumber = $row['max'];