SELECT MAX(...在 PHP/MYSQL 中不返回任何内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12435342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:33:30  来源:igfitidea点击:

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

phpmysqlmax

提问by Samik Sengupta

This is the table structure-

这是表结构-

Table: test

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

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

PAGE是一个具有数据类型的 Primary INT(11)。它不会自动递增。CONTENT 属于数据类型TEXT

In PHP I do-

在 PHP 我做 -

$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 :

没有输出。在所有。如果我做的事情就像echo "Value : ".$row["PAGE"];我看到的那样Value :

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

查询SELECT * FROM test工作得很好。我在某处使用MAX()语法错了吗?

I want it to return the maximum value of PAGEas of yet.

我希望它返回PAGE迄今为止的最大值。

回答by FluffyHyman

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

回答by paxdiablo

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.

你不应该在该查询周围加上引号mysql_query吗?我不知道 PHP 会用这样一个语法不足的语句做什么,我原以为它会给你一个错误。

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:

在任何情况下,聚合函数的列名可能与用于它的列不同(从内存中,DB2 给它一个与函数相似的名称,例如max_page_或其他)。您可能希望通过强制使用以下名称来确保它具有正确的列名称:

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

回答by Vijai Sebastian

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_datais my table,and page2_content_idis the column name

$con=new mysqli($server,$user,$password,$db_name);page2_content_data是我的表,page2_content_id是列名

回答by Bruce Ballad

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.

这里的区别在于使用 WHERE 来选择一个组。

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

而且mysql_result($maxscore_query, 0);对我来说更容易管理。

回答by im-sunil

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