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
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

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.