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.
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.
MySQL MAX() Function The MAX() function returns the maximum value in a set of values. Note: See also the MIN() function.
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,...)
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.
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"];
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