Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT MAX(... not returning anything in PHP/MYSQL

Tags:

php

mysql

max

This is the table structure-

Table: test

+------+---------+
| PAGE | CONTENT |
+------+---------+
|  1   |   ABC   |
+------+---------+
|  2   |   DEF   |
+------+---------+
|  3   |   GHI   |
+------+---------+

PAGE is a Primary with datatype INT(11). It does not auto-increment. CONTENT is of the datatype TEXT.

In PHP I do-

$result = mysql_query(SELECT MAX(PAGE) FROM test);
$row = mysql_fetch_array($result);
echo $row["PAGE"];

No output. At all. If I do something like echo "Value : ".$row["PAGE"]; all I see is Value :

The query SELECT * FROM test works just fine though. Am I wrong somewhere using the MAX() syntax?

I want it to return the maximum value of PAGE as of yet.

like image 695
Samik Sengupta Avatar asked Sep 15 '12 07:09

Samik Sengupta


People also ask

Why Max function is not working in MySQL?

Your query contains fields in the SELECT clause which are not part of the GROUP BY clause. In standard SQL, a query that includes a GROUP BY clause cannot refer to nonaggregated columns in the select list that are not named in the GROUP BY clause.

What is a function to return max value in MySQL?

MySQL MAX() Function The MAX() function returns the maximum value in a set of values. Note: See also the MIN() function.

How do I find max 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,...)

What does Max () mean in SQL?

SQL MIN() and MAX() Functions The MIN() function returns the smallest value of the selected column. The MAX() function returns the largest value of the selected column.


1 Answers

This should be the code.

$result = mysql_query("SELECT MAX(PAGE) AS max_page FROM test");
$row = mysql_fetch_array($result);
echo $row["max_page"];
like image 127
FluffyJack Avatar answered Oct 08 '22 05:10

FluffyJack