Jump to content

getting most records by count


DrTrans

Recommended Posts

<?php 
    
     $connect = mysql_connect("localhost","damgears_evil","damgears");
     mysql_select_db("damgears_evil");
     $queryreg = mysql_query("SELECT COUNT(*) FROM titles WHERE fname = '$fname' && lname = '$lname' ");
     	 while($row = mysql_fetch_assoc($queryreg))
	{
		$amountitles = $row[0];
		$dbname = $row['fname'];
         echo "$amoutitles $dbname";
         }
    ?>

 

Basically what i want to do is if ther is a record

 

Jeff Smith

Jeff Smith

Jeff Smith

John Adams

John Adams

Susan Adams

Jeff Smith

 

I want it to count it and order it by count so the person with the highest amount of records then displays

 

$amounttitles - $dbname

 

and limit to top 10.

 

thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/178002-getting-most-records-by-count/
Share on other sites

SELECT count(*) FROM titles WHERE fname = '$fname' && lname = '$lame' ORDER BY count(*) GROUP BY fname,lname

 

I haven't tested this. I dont remember if group has to be before or after order or if it matters.

 

Group by is before.  Also these queries will display a number, but nothing else.  If you actually want to see a name, you need it in the result set. Something like this:

 

SELECT fname, lname, count(*) as countOf FROM titles WHERE fname = '$fname' AND lname = '$lname' GROUP BY fname, lname ORDER BY countOf DESC LIMIT 0, 10

$connect = mysql_connect("localhost","damgears_evil","damgears");
     mysql_select_db("damgears_evil");
     $queryreg = mysql_query("SELECT fname, lname, count(*) as countOf FROM titles WHERE fname = '$fname' AND lname = '$lname' GROUP BY fname, lname ORDER BY countOf DESC LIMIT 0, 10");
     	 while($row = mysql_fetch_assoc($queryreg))
	{
		$amountitles = $row[0];
		$dbname = $row['fname'];
         echo "$amountitles $dbname";
         }

 

I did that, and nothign happens. I am very confused.

If *nothing* is happening, then it's likely the query is failing or there just are no valid results. You should add some error checking to find out which it is. This...

 

$sql = "SELECT fname, lname, count(*) as countOf FROM titles WHERE fname = '$fname' AND lname = '$lname' GROUP BY fname, lname ORDER BY countOf DESC LIMIT 0, 10";
$queryreg = mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR);

... will show you if theres any errors. If it doesn't come up with any errors then try doing...

 

echo mysql_num_rows($queryreg);

... to see if there are actually any rows found.

Ok, I got the results to return the names however:

 

while($row = mysql_fetch_assoc($queryreg))
	{
		$amountitles = $row[0];
		$dbname = $row['fname'];
		$dblname = $row['lname'];
         	print "<center><b><a href=\"profile.php?fname=$dbname&lname=$dblname\">$dbname $dblname</a></b><br>$amountitles<br>";
}

 

How do i get the count number so basically $amountitles = Amount of records grouped together.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.