Mysql-a php computing result problem. Please be a master

Source: Internet
Author: User
{Code ...}
// Search for $ SQL = "select * from part_time where agents = 6"; $ result = mysql_query ($ SQL ); while ($ row = mysql_fetch_assoc ($ result) {$ id = $ row ['id']; // cyclically generate all agents = 2 IDs $ resu = "select count (*) from userinfo where part_person = $ id"; $ re = mysql_query ($ resu ); $ roo = mysql_fetch_assoc ($ re); $ number = $ roo ['count (*) ']; // calculates that userinfo is the same part-time employee (part_time) echo $ number; echo "----" ;}// add $ number to get the final number. What should I do? Please answer

Reply content:
// Search for $ SQL = "select * from part_time where agents = 6"; $ result = mysql_query ($ SQL ); while ($ row = mysql_fetch_assoc ($ result) {$ id = $ row ['id']; // cyclically generate all agents = 2 IDs $ resu = "select count (*) from userinfo where part_person = $ id"; $ re = mysql_query ($ resu ); $ roo = mysql_fetch_assoc ($ re); $ number = $ roo ['count (*) ']; // calculates that userinfo is the same part-time employee (part_time) echo $ number; echo "----" ;}// add $ number to get the final number. What should I do? Please answer

InwhileThe preceding definition is as follows:$numberAnd then directly$number+=$row['count(*)']You can.

In addition, I simplified the code for you...

$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'];

Defines $ sum = 0;
Add $ sum + = $ number to while

$ Number + = $ roo ['count (*) '];

Wow, when you repeatedly call the database in a loop statement, it is estimated that the database will not be able to withstand the high traffic volume. You can use join queries.

Try aggregate.

$number += $roo['count(*)'];

But this code is a 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.