下面什么都不显示。我只是想拉出两个日期之间的最大数字。 SQL 查询实际上可以工作,并且日期很好,因为它们在其他查询中使用。它似乎不适用于 MAX
选择器。
$con = mysql_connect("host","user","pw");
mysql_select_db("db", $con);
//Other queries before this..
$query5="SELECT MAX(TOTALVISITS) FROM mytable WHERE DATE between '$mystartdate' and '$thedbdate'";
$result5=mysql_query($query5);
mysql_close();
$maxtotal=mysql_result($result5);
echo $MAX TOTAL: " . $maxtotal;
最佳答案
首先,不要在 mysql_query()
之后调用 mysql_close()
。您的结果集将无法获取。
$result5=mysql_query($query5);
// DON'T DO THIS!
//mysql_close();
mysql_result()
至少需要两个参数 - 资源和要检索的行,以及(可选)要检索的列。由于您只有一个,因此技术上可以省略该列。
// Get the first column of the first row from the result set.
$maxvisits = mysql_result($result5, 0, 0);
请注意,最好为计算列或聚合列分配别名:
$query5="SELECT MAX(TOTALVISITS) AS maxvisits FROM mytable WHERE DATE between '$mystartdate' and '$thedbdate'";
//------------------------------^^^^^^^^^^^^^^
这使得使用 mysql_fetch_assoc()
等函数变得更加容易,它比 mysql_result() 更灵活一点:
$row = mysql_fetch_assoc($result5);
echo $row['maxvisits'];
关于php MAX MySQL 查询不回显,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9740592/