PHP Mysql COUNT导致双循环

时间:2013-04-19 13:04:27

标签: php mysql loops count while-loop

我想知道如果我有两个循环,我如何计算结果! :

$select = "SELECT X FROM Y"

$result = mysql_query($select,$link);

$total = mysql_num_rows($result);

while($row = mysql_fetch_array($result)) {

    $Name = $row['X'];
    $select2 = "SELECT id FROM `".$Name."` ";
    $result2 = mysql_query($select2,$link) or die ('Erreur : '.mysql_error() );
    $total2 = mysql_num_rows($result2);
    while($row2 = mysql_fetch_array($result2)) { echo $row2['id']} }

我想算一下我最后有多少身份证明。谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

给这个

$select = "SELECT X FROM Y"

$result = mysql_query($select,$link);

$total = mysql_num_rows($result);

$finalTotal=0;
while($row = mysql_fetch_array($result)) {

$Name = $row['X'];
$select2 = "SELECT id FROM `".$Name."` ";
$result2 = mysql_query($select2,$link) or die ('Erreur : '.mysql_error() );
$total2 = mysql_num_rows($result2);
 $finalTotal = $finalTotal + $total2;
while($row2 = mysql_fetch_array($result2)) { echo $row2['id'];} }

echo "total id is ".$finalTotal;

$ finalTotal将显示id的总数