1

目前我有以下代码:

$forummost = mysql_query("

 SELECT door, 
 COUNT(door) AS doorCount 
 FROM forum_posts 
 GROUP BY door 
 ORDER BY COUNT(door) 
 DESC

");
$forummostc = mysql_num_rows($forummost);
$forummostf = mysql_fetch_assoc($forummost);

有人能解释一下为什么mysql_num_rows($forummost)不起作用吗?

提前致谢!

4

3 回答 3

1

mysql_num_rows计算查询返回的行数。您的查询是what is the count for each door每扇门只返回一行。

于 2012-04-10T19:36:47.963 回答
1

GROUP BY只选择 mysql 中该列的不同值。在您的情况下,正如所指出的那样,它会选择尽可能多的行,因为door存在不同的值。

于 2012-04-10T19:41:36.097 回答
0

试试这个,我测试过,它有效..

<?php 
$link = mysql_connect('localhost','root',''); 
if (!$link) 
{ 
    die('Could not connect to MySQL: ' . mysql_error()); 
} 
echo 'Connection OK'."</br>"; 


$forummost = mysql_query("
    SELECT door, 
COUNT(door) AS doorCount 
FROM forum_posts 
GROUP BY door 
ORDER BY COUNT(door) 
DESC
");


$forummostc = mysql_num_rows($forummost);

if ($forummostc == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

while ($row = mysql_fetch_assoc($forummost)) {
    echo $row["door"];
    echo $row["doorCount"];

}

mysql_free_result($forummost);

mysql_close($link); 

?> 
于 2012-04-10T19:43:41.623 回答