PHP从多个表中选择并一起添加值以获得总计

时间:2014-01-23 22:02:31

标签: php mysql

我正在尝试在PHP中运行这些查询到mysql表

$sql="SELECT * from customer where customerid > '' and voicemail_max > '' and company_status = '' ";
$rs=mysql_query($sql,$conn);
while($customer=mysql_fetch_array($rs)) {
    $sql2="SELECT * from client where parent_client_id = '".$customer["customerid"]."' ";
    $rs2=mysql_query($sql2,$pbx01_conn);
    while($result2=mysql_fetch_array($rs2))
    {
        $sql3="SELECT * from extension where client_id = '".substr($result2["id"],1,3)."' ";
        $rs3=mysql_query($sql3,$pbx01_conn);
        while($result3=mysql_fetch_array($rs3))
        {
            $sql4="SELECT * from extension_voicemail where extension_id = '".$result3["id"]."' ";
            $rs4=mysql_query($sql4,$pbx01_conn);
            $voicemail_size_total=0;
            while($result4=mysql_fetch_array($rs4)) {
                $voicemail_size_total = $voicemail_size_total+$result4["filesize"];
            }
        }
    }
    echo $voicemail_size_total;
}

它应该是从一列添加多个值

$sql4查询如下:

SELECT * from extension_voicemail where extension_id = '1454' 

当我在数据库中运行它时,它返回一行,文件大小列为31780

但是当我回显$voicemail_size_total变量时,它什么也没有显示。

如果我将该变量从它所在的位置回显一行,那么它的工作正常,但它现在什么也没有显示

2 个答案:

答案 0 :(得分:1)

在所有while循环之外声明$voicemail_size_total=0;。每当你回到循环时,你都会将其重新设置为0.这可能就是问题所在。

答案 1 :(得分:0)

嗯,这取决于你想要达到的目标: 1.如果你想获得所有文件大小(基本上你只想要一个值),试试这个:

$sql="SELECT * from customer where customerid > '' and voicemail_max > '' and company_status = '' ";
$rs=mysql_query($sql,$conn);
$voicemail_size_total=0;
while($customer=mysql_fetch_array($rs)) {
    $sql2="SELECT * from client where parent_client_id = '".$customer["customerid"]."' ";
    $rs2=mysql_query($sql2,$pbx01_conn);
    while($result2=mysql_fetch_array($rs2))
    {
        $sql3="SELECT * from extension where client_id = '".substr($result2["id"],1,3)."' ";
        $rs3=mysql_query($sql3,$pbx01_conn);
        while($result3=mysql_fetch_array($rs3))
        {
            $sql4="SELECT * from extension_voicemail where extension_id = '".$result3["id"]."' ";
            $rs4=mysql_query($sql4,$pbx01_conn);
            while($result4=mysql_fetch_array($rs4)) {
                $voicemail_size_total = $voicemail_size_total+$result4["filesize"];
            }
        }
    }
}
echo $voicemail_size_total;

2。如果您正在寻找每个客户的文件大小(即期望呈现一系列文件大小),请尝试以下方法:

$sql="SELECT * from customer where customerid > '' and voicemail_max > '' and company_status = '' ";
$rs=mysql_query($sql,$conn);
while($customer=mysql_fetch_array($rs)) {
    $sql2="SELECT * from client where parent_client_id = '".$customer["customerid"]."' ";
    $rs2=mysql_query($sql2,$pbx01_conn);
    $voicemail_size_total=0;
    while($result2=mysql_fetch_array($rs2))
    {
        $sql3="SELECT * from extension where client_id = '".substr($result2["id"],1,3)."' ";
        $rs3=mysql_query($sql3,$pbx01_conn);
        while($result3=mysql_fetch_array($rs3))
        {
            $sql4="SELECT * from extension_voicemail where extension_id = '".$result3["id"]."' ";
            $rs4=mysql_query($sql4,$pbx01_conn);
            while($result4=mysql_fetch_array($rs4)) {
                $voicemail_size_total = $voicemail_size_total+$result4["filesize"];
            }
        }
    }
    echo $voicemail_size_total;
}