Jump to content

counting issues


mknjhill

Recommended Posts

I need some help, I have a basic background to php, but know basically nothing about mysql..

 

here is what i wrote:

session_start();
include("conn.php");

	$result2 = mysql_query("SELECT domain,domainstatus FROM ".$table_name);

	while($row2 = mysql_fetch_array($result2))
	  {
		if($row2[1]=="Active"){
			echo $row2[1];


		}
	  }
mysql_close();
error_reporting(1);

Right now you see "ActiveActiveActiveActiveActiveActiveActiveActive"

How in the world do i show "8"  I tried to use count()  but then i would get 1111111111111111 (16 1's, why double) ???

 

I'm just trying to show a number of active services...  any help is appreciated :)

  • MySQL server version -- 5
Link to comment
Share on other sites

Change

while($row2 = mysql_fetch_array($result2))
     if($row2[1]=="Active"){
          echo $row2[1];
     }
}

to

$i = 0;
while($row2 = mysql_fetch_array($result2))
{
     if($row2[1]=="Active"){
          $i++;
     }
} 
echo $i;

What's happening here is you define the variable $i as 0, then every time row2[1] equals Active you notch it up 1. Then after the loop you echo the count. This is how you could count it in PHP but it's more efficient and effective to do it the way Barand mentioned. SQL will add up the total number of rows selected by that query and you could echo it by $rw['total'];

Edited by wwwroth
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.