SELECT MAX (... does not return anything in PHP / MYSQL

This is the table structure -

Table: test +------+---------+ | PAGE | CONTENT | +------+---------+ | 1 | ABC | +------+---------+ | 2 | DEF | +------+---------+ | 3 | GHI | +------+---------+ 

PAGE is Primary with an INT(11) data type. It is not auto increment. CONTENT refers to the TEXT data type.

In PHP, I do -

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

There is no conclusion. At all. If I do something like echo "Value : ".$row["PAGE"]; , all I see is Value :

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

I want it not to return the maximum PAGE value yet.

+6
source share
4 answers

It must be a code.

 $result = mysql_query("SELECT MAX(PAGE) AS max_page FROM test"); $row = mysql_fetch_array($result); echo $row["max_page"]; 
+8
source

Don't you have quotes around this query in mysql_query ? I have no idea what PHP would do with such a syntactically inadequate expression, I would think that would give you an error.

In any case, the aggregated function may have a different column name than the column used for it (from the memory, DB2 gives it a similar name for the function, for example max_page_ or something else). You can make sure 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
source
 $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
source

Try entering the 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
source

Source: https://habr.com/ru/post/925453/


All Articles