Jump to content

Counting instances of row field


frobak

Recommended Posts

Hi

 

I am trying to count the number of instances of a field in a database table. So basically i want it to count how many times a value occurs and group them by value. I then want to display each count instance seperately. How would i get the result from the array and into a variable so i can use it?

 

				<?php
			include("includes/db_connect.inc.php");

			$sql = "SELECT downloadid, COUNT(*) FROM downloadcount GROUP BY downloadid ";
			$result = mysql_query($sql);

			$row = mysql_fetch_array($result);

			$num1 = mysql_num_rows($row);
			?>

Link to comment
https://forums.phpfreaks.com/topic/149568-counting-instances-of-row-field/
Share on other sites

mysql_fetch_array only fetches a single row and moves the cursor one position forward. When there are no more rows it returns false. You can use a while loop to get all the rows. The loop will run as long as the condition is true and will therefore terminate when there are no rows left.

 

while ($row = mysql_fetch_array($result)) {
// do as you want with each $row here
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.