Jump to content
Sign in to follow this  
dcole.ath.cx

Number of Rows in Results

Recommended Posts

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?

Share this post


Link to post
Share on other sites

You can use mysql_num_rows(), e.g.

$query  = 'SELECT message, response FROM $groupsconvert[$o]';$result = mysql_query($query);echo mysql_num_rows($result);

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

It's nice to do a search and find that someone else had the exact same question.

Edited by niche

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.t_closed.gif

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

×
×
  • Create New...