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
https://forums.phpfreaks.com/topic/281399-counting-issues/
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'];

Link to comment
https://forums.phpfreaks.com/topic/281399-counting-issues/#findComment-1446183
Share on other sites

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.