suttercain Posted November 27, 2007 Share Posted November 27, 2007 I currentley have to SQL statements to get the number of rows found (mysql_num_rows) like this.. <?php $sql = mysql_query ("SELECT names FROM table WHERE name ='Frank'") $count = mysql_num_rows($sql); echo $count; //Works as desired. $sql = mysql_query ("SELECT names FROM table WHERE name ='John'") $count = mysql_num_rows($sql); echo $count; //Works as desired. ?> Can I somehow do a "row count" in another fashion and with a single mysql_query? <?php $sql = mysql_query ("SELECT names FROM table") //somehow show number of John's found. //somehow show number of Frank's found. ?> Would in_array somehow come into play with what I am trying to achieve? Thanks. SC Quote Link to comment Share on other sites More sharing options...
clanstyles Posted November 27, 2007 Share Posted November 27, 2007 Well you can do things like: $sql = mysql_query ("SELECT COUNT(*) FROM table WHERE name ='Frank'"); If thats what you mean then yes otherwise I don't understand what you mean exactly. Quote Link to comment Share on other sites More sharing options...
suttercain Posted November 27, 2007 Author Share Posted November 27, 2007 Hi Clan styles, Thanks for reply. I am trying only have to have one single mysql_query call without the WHERE clause and be able to find specific times a specific item is found in the database. Quote Link to comment Share on other sites More sharing options...
revraz Posted November 27, 2007 Share Posted November 27, 2007 Well you can do query's like this SELECT names FROM table WHERE name='Frank' or name='John' You can then fetch the results into an Array or Object and sort it. Quote Link to comment Share on other sites More sharing options...
clanstyles Posted November 27, 2007 Share Posted November 27, 2007 Well you can do query's like this SELECT names FROM table WHERE name='Frank' or name='John' You can then fetch the results into an Array or Object and sort it. without the WHERE clause lol idk why that seems stupid to me without a were clause but heh. Let MYSQL do as much work as possible Quote Link to comment Share on other sites More sharing options...
revraz Posted November 27, 2007 Share Posted November 27, 2007 SELECT names FROM table Would load all names then use a array to sort. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 27, 2007 Share Posted November 27, 2007 SELECT names FROM table Would load all names then use a array to sort. Don't take this the wrong way, but this is a horrible suggestion. You are going to get many more items than you need and force PHP to work way harder than it has to. If the table is very large this approach can kill the site. First off, as someone else suggested, doing a SELECT COUNT(*) .. is much more efficient than selecting everything and then calling mysql_num_rows(). If all you want is a count, do not ever select everything and then count the size of the result set; the reason you do not do this is because MySQL will have to send a potentially large result-set of data when all you want is a single number, very inefficient! As for your particular question: SELECT COUNT(*) AS `n`, `name` FROM `table` GROUP BY `name` ORDER BY `name` And if you only want specific names: SELECT COUNT(*) AS `n`, `name` FROM `table` WHERE `name` IN ('Frank', 'Bob', 'Kelly') GROUP BY `name` ORDER BY `name` Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 27, 2007 Share Posted November 27, 2007 what I like to do is <?php $q = "select COUNT(*) from `Table` Where this='that'"; $r = mysql_query($q) or die(mysql_error()); $count = mysql_result($r,0); ?> you can't do anything with the query without doing one more step, using mysql_num_rows or mysql_result are probably going to be faster than mysql_fetch_array(), but we are talking microseconds here Quote Link to comment Share on other sites More sharing options...
hamza Posted November 28, 2007 Share Posted November 28, 2007 <?php $sql = mysql_query ("SELECT names FROM table WHERE name ='Frank'") $count = mysql_num_rows($sql); echo $count; //Works as desired. $sql = mysql_query ("SELECT names FROM table WHERE name ='John'") $count = mysql_num_rows($sql); echo $count; //Works as desired. ?> Can I somehow do a "row count" in another fashion and with a single mysql_query? Code: <?php $sql = mysql_query ("SELECT names FROM table") //somehow show number of John's found. //somehow show number of Frank's found. ?> ---------------------------------------------------------------------------------- if you want to row count with one query just simply do in that way $sql = mysql_query ("SELECT count(names) FROM table"); Simply put the column in the mysql count function and know the number of rows in any coulmn. if still you are facing any problem than send me your question in detail. thats it 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.