MySQL-a problem that calculates PHP results. Daniel, Seek Master

Source: Internet
Author: User
//从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 相加得到最终的数字。应该怎么做?求大牛解答

whiledefine 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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.