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
The max() function is an in-built function in PHP. You can use the max() function to retrieve the maximum value and display that value on-page. If you want to fetch the minimum number from the MySQL database table, then use the min() function of PHP.
PHP's code to find the minimum and maximum salary using min() and max() functions.
If you're working with MySQL, you can combine MAX() with the GREATEST() function to get the biggest value from two or more fields. Here's the syntax for GREATEST: GREATEST(value1,value2,...) Given two or more arguments, it returns the largest (maximum-valued) argument.
The MAX() function returns the largest value of the selected column.
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'];
I believe SELECT max(id) FROM table
will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With