//从part_time数据库中查找$sql="select * from part_time where agents=6";$result=mysql_query($sql);while($row=mysql_fetch_assoc($result)){ $id=$row['id'];//循环出所有agents=2的id $resu="select count(*) from userinfo where part_person=$id"; $re=mysql_query($resu); $roo=mysql_fetch_assoc($re); $number= $roo['count(*)']; //计算出userinfo中是相同兼职人员(part_time)的人数 echo $number; echo "----";}//现在需要把$number 相加得到最终的数字。应该怎么做?求大牛解答
Reply content:
//从part_time数据库中查找$sql="select * from part_time where agents=6";$result=mysql_query($sql);while($row=mysql_fetch_assoc($result)){ $id=$row['id'];//循环出所有agents=2的id $resu="select count(*) from userinfo where part_person=$id"; $re=mysql_query($resu); $roo=mysql_fetch_assoc($re); $number= $roo['count(*)']; //计算出userinfo中是相同兼职人员(part_time)的人数 echo $number; echo "----";}//现在需要把$number 相加得到最终的数字。应该怎么做?求大牛解答
while
define one in the front $number
and then directly $number+=$row['count(*)']
.
In addition, I will give you the code to simplify the next ...
$sql = 'select count(*) as total from userinfo where part_person IN (select id from part_time where agents = 6)';$result = mysql_query($sql);$row = mysql_fetch_assoc($result);echo $row['total'];
Define a $sum=0;
Add $sum+= in while
+ = $roo [' Count (*) '];
Wow, in the loop you call the database so repeatedly, the access to the large estimate database will be unbearable. You can use join queries.
Try aggregate.
$number += $roo['count(*)'];
But this code is a little bit.