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
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
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.