Talon
Talon

Reputation: 4995

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

Upvotes: 10

Views: 54432

Answers (6)

Dipesh
Dipesh

Reputation: 397

Try This Query

 "SELECT MAX(Price) AS HighestPrice FROM Products";

Upvotes: 1

Anand
Anand

Reputation: 1021

Use MAX(ID) to get the largest value

Upvotes: 0

xthexder
xthexder

Reputation: 1565

You can do a single query to figure this out:

http://www.tutorialspoint.com/mysql/mysql-max-function.htm

MySql has it's own Max function that will return the highest value in the specified column.

Upvotes: 0

hkf
hkf

Reputation: 4520

SELECT MAX(ID) FROM TABLE

Execute the statement and assign it to your variable.

Upvotes: 1

qitch
qitch

Reputation: 829

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

Upvotes: 2

hjpotter92
hjpotter92

Reputation: 80639

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'];

Upvotes: 25

Related Questions