2014-01-23 79 views
0

我試圖運行PHP這些查詢到MySQL表從多個表PHP選擇並添加值一起獲得總

$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變量時,它什麼都沒顯示。

它工作正常,如果我贊同從那裏是可變的一條線,但它現在是顯示什麼

回答

1

聲明$voicemail_size_total=0;以外的所有while循環的。每次你回到循環時,你都將它重新設置爲0.這可能是問題所在。

0

嗯,這取決於你想要達到什麼目的: 1.如果你想獲得一個總所有的filesizes(basicly你期待只有一個值呈現)的嘗試:

$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; 
} 
相關問題