SkyRanger Posted February 5, 2013 Share Posted February 5, 2013 I am trying to count rows where there is multiple entries. userlevels: (column name) 2 3 4 I can get them to post separate What I need to do is do a row count with 2 of the number 3 and 4 example: userlevels: 2 - 1 Entry 3 - 2 entries 4 - 3 entries if ($resultt = $mysqli->query("SELECT userlevel FROM users where userlevel='3' and userlevel='4' ")) { /* determine number of rows result set */ $row_cntt = $resultt->num_rows; printf("%d", $row_cntt); /* close result set */ $resultt->close(); } I got it so: Level 2 : 1 User Other: 0 Users but as stated there are 2 for 3 and 3 for 4 Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 5, 2013 Share Posted February 5, 2013 You'll need to use COUNT() and GROUP BY in your query. Check out the MySQL documentation and give it a shot. Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted February 5, 2013 Author Share Posted February 5, 2013 (edited) Thank Jessica, ok Tried this and still an output of 0 if ($resultt = $mysqli->query("SELECT userlevel, count(*) FROM users where userlevel='3' and userlevel='4' group by userlevel")) { /* determine number of rows result set */ $row_cntt = $resultt->num_rows; printf("%d", $row_cntt); /* close result set */ $resultt->close(); } Edited February 5, 2013 by SkyRanger Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 5, 2013 Share Posted February 5, 2013 (edited) A user level can't be both equal to 3 and equal to 4. There is no such number. Furthermore, if you want the output you said in your first post, you don't want the number of rows returned, you want the actual rows. Edited February 5, 2013 by Jessica Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted February 5, 2013 Author Share Posted February 5, 2013 Yeah I need the actual number of rows that are userlevel 3 & 4 so if there is 2 rows with level 3 and 3 rows for level 4, I need it to output 5. So would it be easier to output the count for each one separate then add them together? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 5, 2013 Share Posted February 5, 2013 (edited) A number cannot be both 3 AND 4. It could be ..... 3 OR 4.... If you just want the entire count remove the group by. Edited February 5, 2013 by Jessica Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted February 5, 2013 Author Share Posted February 5, 2013 Figured out how to do it the easy way. because I already had the count for 3 and 4 displaying in my script. I just added them together to get the sum echo $row_cntf+ $row_cntp; cnttf - is the count for 3 cntp - is the count for 4 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.