10

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.

3
  • 4
    Everything seems all right..could you try using this query SELECT MAX(PAGE) as PAGE FROM test instead and see if theres any difference? Commented Sep 15, 2012 at 7:14
  • Thanks a lot. It works. Been struggling with this for a long time. Commented Sep 15, 2012 at 7:16
  • 3
    Please stop writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the deprecation process. Instead you should learn about prepared statements and use either PDO or MySQLi. If you care to learn, here is a quite good PDO-related tutorial. Commented Sep 15, 2012 at 7:16

5 Answers 5

18

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"];
Sign up to request clarification or add additional context in comments.

Comments

2

Shouldn't you have quotes around that query in mysql_query? I have no idea what PHP will do with such a syntactically inadequate statement, I would have thought it would have given you an error.

In any case, an aggregate function may have a different column name than the column used for it (from memory, DB2 gives it a similar name to the function, like max_page_ or something). You may want to ensure it has the correct column name by forcing the name with something like:

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

1 Comment

I think he just mistyped it, as he'd be getting an error not just nothing otherwise.
1

Try below code

$result = mysqli_query($con,"SELECT max(page2_content_id) AS max_page from page2_content_data");
$row = mysqli_fetch_array($result);
echo $row["max_page"];

Where $con=new mysqli($server,$user,$password,$db_name); and page2_content_data is my table,and page2_content_id is the column name

1 Comment

Users of SO can order answers in several ways, so speaking of "above answer and below" is mostly meaningless.
0
$connect = mysqli_connect("localhost", "root", "", "carBid") or die("not connected");

//connection to database
$sql2 = "SELECT max(mybid) FROM `bid`";

//simle select statement with max function
$result_set2 = mysqli_query($connect,$sql2);

//query a result fetch
if ($result_set2) {
    $rowB = mysqli_fetch_array($result_set2);
    //feching a result in array format
    echo $rowB['max(mybid)'];
    //accessing array by name of column with max() function of mysql
} else {
    echo 'No Current Bid';
}
mysqli_close($connect);

4 Comments

There is one "{" too much or too less, I think.
Just a quick note. mysql_query and mysql_fetch_array are deprecated.
While this code may answer the question, it would be better to explain how it solves the problem and why to use it. Code-only answers are not useful in the long run.
<?php $connect= mysqli_connect("localhost", "root", "", "carBid") or die("not connected"); //connection to database $sql2 = "SELECT max(mybid) FROM bid"; //simle select statement with max function $result_set2 = mysqli_query($connect,$sql2); //query a result fetch if ($result_set2) { $rowB = mysqli_fetch_array($result_set2); //feching a result in array format echo $rowB['max(mybid)']; //accessing array by name of column with max() function of mysql } else { echo 'No Current Bid'; } mysqli_close($connect); ?>
0

I used something like this in my code;

$maxscore_query = mysql_query("SELECT MAX(`score`) FROM `allscores` WHERE`level`='$levelcode'");

echo mysql_result($maxscore_query, 0);

The difference here is the use of WHERE for selecting a group.

And mysql_result($maxscore_query, 0); is easier to manage for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.