1

At the moment I've got the following code:

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

Can somebody explain me why mysql_num_rows($forummost) doesn't work?

Thanks in advance!

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Mossawi
  • 871
  • 2
  • 10
  • 17

3 Answers3

1

mysql_num_rows counts the number of rows returned by the query. Your query is what is the count for each door so only one row for each door is returned.

webbiedave
  • 48,414
  • 8
  • 88
  • 101
1

GROUP BY selects only the different values of that column in mysql. In your case, as pointed out, it selects as many rows as different door values are there.

Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90
0

try this, I tested, it works..

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

?> 
Nesim Razon
  • 9,684
  • 3
  • 36
  • 48