Samik Sengupta
Samik Sengupta

Reputation: 1973

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

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.

Upvotes: 10

Views: 51335

Answers (5)

Bruce Ballad
Bruce Ballad

Reputation: 106

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.

Upvotes: 0

Vijai Sebastian
Vijai Sebastian

Reputation: 127

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

Upvotes: 1

im-sunil
im-sunil

Reputation: 434

$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);

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 882336

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

Upvotes: 2

FluffyJack
FluffyJack

Reputation: 1732

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

Upvotes: 18

Related Questions