Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the Largest Number in a mySQL Database in PHP

Tags:

php

mysql

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

like image 318
Talon Avatar asked Mar 21 '12 03:03

Talon


People also ask

How to find biggest Number in database in PHP?

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.

How to get max value in MySQL using PHP?

PHP's code to find the minimum and maximum salary using min() and max() functions.

How do I find the highest number in MySQL?

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.

How do you find the highest number in SQL?

The MAX() function returns the largest value of the selected column.


2 Answers

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'];
like image 87
hjpotter92 Avatar answered Sep 24 '22 20:09

hjpotter92


I believe SELECT max(id) FROM table will work.

like image 34
qitch Avatar answered Sep 22 '22 20:09

qitch