timmah1 Posted October 8, 2008 Share Posted October 8, 2008 I have a database that inputs the users name and either correct or wrong Each user could have up to 100 entries in the database. How can pull that information from the database with the user name being listed just once, and how many that user has correct? Just like forums, it shows the user and how many posts they have. So it would look like this: User - (87) Instead of like this: User - (87) User - (87) User - (87) User - (87) User - (87) User - (87) User - (87) etc... Thank you in advance. Link to comment https://forums.phpfreaks.com/topic/127596-solved-list-replies-by-user/ Share on other sites More sharing options...
F1Fan Posted October 8, 2008 Share Posted October 8, 2008 SELECT user, SUM(othercolumn) FROM table GROUP BY user Link to comment https://forums.phpfreaks.com/topic/127596-solved-list-replies-by-user/#findComment-660195 Share on other sites More sharing options...
DarkWater Posted October 8, 2008 Share Posted October 8, 2008 SELECT username, COUNT(*) AS total FROM table_name WHERE answer='correct' GROUP BY username ORDER BY total DESC; I was obviously speculating on the column names and stuff, but you get the idea. Link to comment https://forums.phpfreaks.com/topic/127596-solved-list-replies-by-user/#findComment-660197 Share on other sites More sharing options...
timmah1 Posted October 8, 2008 Author Share Posted October 8, 2008 Thanks. I have this $users = mysql_fetch_assoc(mysql_query("SELECT name,answer COUNT(*) AS total FROM daily WHERE answer = 'Correct' GROUP BY name ORDER BY total DESC")); echo $users['total']; But I keep getting an error Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource Link to comment https://forums.phpfreaks.com/topic/127596-solved-list-replies-by-user/#findComment-660216 Share on other sites More sharing options...
DarkWater Posted October 8, 2008 Share Posted October 8, 2008 I'd think you'd know what to do...read the quote in your signature. =P Link to comment https://forums.phpfreaks.com/topic/127596-solved-list-replies-by-user/#findComment-660220 Share on other sites More sharing options...
timmah1 Posted October 8, 2008 Author Share Posted October 8, 2008 D'oh!! Link to comment https://forums.phpfreaks.com/topic/127596-solved-list-replies-by-user/#findComment-660222 Share on other sites More sharing options...
DarkWater Posted October 8, 2008 Share Posted October 8, 2008 Okay, seeing as you marked this as Solved, I'd assume you got it all working? Link to comment https://forums.phpfreaks.com/topic/127596-solved-list-replies-by-user/#findComment-660230 Share on other sites More sharing options...
timmah1 Posted October 8, 2008 Author Share Posted October 8, 2008 I thought I had it figured out. What am I doing wrong here? It's only showing one result $csql = mysql_fetch_assoc(mysql_query("SELECT name,answer, COUNT(*) AS total FROM daily WHERE answer = 'Correct' GROUP BY name ORDER BY total DESC")) OR die(mysql_error());; echo $csql['name']; echo $csql['total']; I need it to show every user that's in the database with the number beside it Link to comment https://forums.phpfreaks.com/topic/127596-solved-list-replies-by-user/#findComment-660280 Share on other sites More sharing options...
DarkWater Posted October 8, 2008 Share Posted October 8, 2008 You'd need to loop through the results...Also, don't use mysql_fetch_assoc() directly on the result resource from mysql_query(). Save it to a variable. =/ $csql = mysql_query("SELECT name,answer, COUNT(*) AS total FROM daily WHERE answer = 'Correct' GROUP BY name ORDER BY total DESC") OR die(mysql_error()); while ($row = mysql_fetch_assoc($csql)) { echo $row['name'] . '-' . $row['total'] . "<br />\n"; } Link to comment https://forums.phpfreaks.com/topic/127596-solved-list-replies-by-user/#findComment-660285 Share on other sites More sharing options...
timmah1 Posted October 8, 2008 Author Share Posted October 8, 2008 Thank you once again DarkWater, you are a life saver. This is now solved!! Link to comment https://forums.phpfreaks.com/topic/127596-solved-list-replies-by-user/#findComment-660291 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.