dcole.ath.cx Posted September 15, 2007 Share Posted September 15, 2007 If I have something like this: $query = 'SELECT message, response FROM $groupsconvert[$o]';$result = mysql_query($query); How can I count how many rows I have? I'm trying to pick a value of a random row. I really don't want to use a while loop and count it. I think I could use: $countresult = count(mysql_fetch_row($result)); But will that could just the current string or is that going to count the number of rows in result? Link to comment Share on other sites More sharing options...
Synook Posted September 15, 2007 Share Posted September 15, 2007 You can use mysql_num_rows(), e.g. $query = 'SELECT message, response FROM $groupsconvert[$o]';$result = mysql_query($query);echo mysql_num_rows($result); Link to comment Share on other sites More sharing options...
justsomeguy Posted September 17, 2007 Share Posted September 17, 2007 You can also use the COUNT aggregate function to count the number of rows using SQL, but it will require one query to do the count and another to get the results.SELECT COUNT(*) AS num FROM table Link to comment Share on other sites More sharing options...
niche Posted October 13, 2010 Share Posted October 13, 2010 It's nice to do a search and find that someone else had the exact same question. Link to comment Share on other sites More sharing options...
Fmdpa Posted October 15, 2010 Share Posted October 15, 2010 Or if you want to SELECT a distinct value from a table where that value might occur more than once, you could do this: SELECT COUNT(DISTINCT column_name) AS num FROM table Link to comment Share on other sites More sharing options...
Synook Posted October 15, 2010 Share Posted October 15, 2010 Gah, niche, you've resurrected a really old thread . We're happy that you found a solution from the archives, but it'll continue to exist even if you don't bump it. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.