mknjhill Posted August 20, 2013 Share Posted August 20, 2013 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 More sharing options...
Barand Posted August 20, 2013 Share Posted August 20, 2013 SELECT COUNT(*) as total FROM `$tablename` WHERE domainstatus = 'Active' Link to comment https://forums.phpfreaks.com/topic/281399-counting-issues/#findComment-1446007 Share on other sites More sharing options...
wwwroth Posted August 21, 2013 Share Posted August 21, 2013 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.