DrTrans Posted October 17, 2009 Share Posted October 17, 2009 <?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 More sharing options...
teynon Posted October 17, 2009 Share Posted October 17, 2009 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. Link to comment https://forums.phpfreaks.com/topic/178002-getting-most-records-by-count/#findComment-938513 Share on other sites More sharing options...
Kaboom Posted October 17, 2009 Share Posted October 17, 2009 would array() work if you set it to show limit (10)? i know how if i reference my files but this is off the top of my bean Link to comment https://forums.phpfreaks.com/topic/178002-getting-most-records-by-count/#findComment-938515 Share on other sites More sharing options...
teynon Posted October 17, 2009 Share Posted October 17, 2009 Didn't see the limit part of the request: SELECT count(*) FROM titles WHERE fname = '$fname' && lname = '$lame' ORDER BY count(*) GROUP BY fname,lname LIMIT 0, 10 Link to comment https://forums.phpfreaks.com/topic/178002-getting-most-records-by-count/#findComment-938517 Share on other sites More sharing options...
gizmola Posted October 17, 2009 Share Posted October 17, 2009 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 https://forums.phpfreaks.com/topic/178002-getting-most-records-by-count/#findComment-938526 Share on other sites More sharing options...
DrTrans Posted October 17, 2009 Author Share Posted October 17, 2009 $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 https://forums.phpfreaks.com/topic/178002-getting-most-records-by-count/#findComment-938628 Share on other sites More sharing options...
cags Posted October 17, 2009 Share Posted October 17, 2009 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 https://forums.phpfreaks.com/topic/178002-getting-most-records-by-count/#findComment-938642 Share on other sites More sharing options...
DrTrans Posted October 17, 2009 Author Share Posted October 17, 2009 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 https://forums.phpfreaks.com/topic/178002-getting-most-records-by-count/#findComment-938651 Share on other sites More sharing options...
cags Posted October 17, 2009 Share Posted October 17, 2009 $row['countOf']; Link to comment https://forums.phpfreaks.com/topic/178002-getting-most-records-by-count/#findComment-938683 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.