tomdchi Posted November 26, 2008 Share Posted November 26, 2008 The query below returns a result for the previous month that contains a list of merchant id's. The $count variable needs to return a value of how many of each $merchantid is contained in the array. What I have returns the total rows. Can someone help me with this please? <?php $query = "select merchantid from tblpmts where date >= date_sub(curdate(), interval 1 month) and date <= date_sub(curdate(), interval 1 day)"; $result = mysql_query($query); while ($data = mysql_fetch_array($result)) { $merchantid = $data["merchantid"]; $count = $data["merchantid"][mysql_num_rows]; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/134386-help-with-array-counting-results/ Share on other sites More sharing options...
sasa Posted November 26, 2008 Share Posted November 26, 2008 try <?php $query = "select merchantid, count(*) as c from tblpmts where date >= date_sub(curdate(), interval 1 month) and date <= date_sub(curdate(), interval 1 day) group by merchantid"; $result = mysql_query($query); while ($data = mysql_fetch_array($result)) { $merchantid = $data["merchantid"]; $count = $data["c"]; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/134386-help-with-array-counting-results/#findComment-699628 Share on other sites More sharing options...
Mark Baker Posted November 26, 2008 Share Posted November 26, 2008 To start with, $merchantid = $data["merchantid"]; is going to overwrite the current value of $merchantid every iteration of the loop. Is that really what you want? <?php $query = "select distinct merchantid from tblpmts where date >= date_sub(curdate(), interval 1 month) and date <= date_sub(curdate(), interval 1 day)"; $result = mysql_query($query); $merchantid = array(); while ($data = mysql_fetch_array($result)) { $merchantid[] = $data["merchantid"]; } $count = count($merchantid); ?> might do what you want Quote Link to comment https://forums.phpfreaks.com/topic/134386-help-with-array-counting-results/#findComment-699630 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.