我知道桌子上有多个结果,有谁能告诉我我做错了什么?
<?php
$con = mysql_connect("x","y","z");
if (!$con)
{
die(mysql_error());
}
mysql_select_db("mydatabase", $con);
$query = ("SELECT COUNT(*) FROM usersonline WHERE datetime > NOW() - INTERVAL 5 MINUTE");
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
echo $num_rows;
?>
答案 0 :(得分:5)
mysql_num_rows
的结果将为一,您只从数据库中选择一行,它恰好是与条件匹配的记录数。试试这个:
$query = ("SELECT COUNT(*) AS cnt FROM usersonline WHERE datetime > NOW() - INTERVAL 5 MINUTE");
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$num_rows = $row['cnt'];
echo $num_rows;
答案 1 :(得分:3)
您正在使用聚合COUNT()函数而没有GROUP BY子句。它会折叠整个结果集并计算记录数,因此您将获得的所有内容都是1行1字段结果集。