没有得到表中的行总和

时间:2013-09-04 08:10:28

标签: php mysql sql

<span>$invoicepartpaid1</span><span>$invoicepartpaid2</span>的结果为空白。应该有钱。有什么建议?

$query="SELECT Distinct company, car_moto, packet FROM companies where userid='$userid' ORDER BY company, car_moto, packet";

$result=mysql_query($query);

$num=mysql_numrows($result);

$i=0;
while ($i < $num) {

$f0=mysql_result($result,$i,"company");
$f1=mysql_result($result,$i,"car_moto");
$f2=mysql_result($result,$i,"packet");

$sql_1 = "SELECT SUM(price_protect) as partpaid1 FROM companies where company='$f0' and car_moto='$f1' and packet='$f2' and userid='$userid'";
$sql_2 = "SELECT SUM(clear_protect) as partpaid2 FROM companies where company='$f0' and car_moto='$f1' and packet='$f2' and userid='$userid'";

$result_1=mysql_query($sql_1) or die('Error query failed');
$result_2=mysql_query($sql_2) or die('Error query failed');

while ($row_1 = mysql_fetch_array($result_1)) {

$invoicepartpaid1 = $row_1['partpaid1'];

}
while ($row_2 = mysql_fetch_array($result_2)) {

$invoicepartpaid2 = $row_2['partpaid2'];

}
    echo "<tr><td align='center'><span>$f0</span></td><td align='center'><span>$f1</span></td><td align='center'><span>$f2</span></td>";
echo "<td align='center'><span>$invoicepartpaid1</span></td><td align='center'><span>$invoicepartpaid2</span></td></tr>";

    $i++;

}

1 个答案:

答案 0 :(得分:1)

替换

$num=mysql_numrows($result);

通过

$num=mysql_num_rows($result);

完整代码

<?php
    $query="SELECT Distinct company, car_moto, packet FROM companies where userid='$userid' ORDER BY company, car_moto, packet";
    $result=mysql_query($query);
    $num=mysql_num_rows($result);
    $i=0;
    while ($row=mysql_fetch_array($result)) {// loop for all results replacing $i < $num
        $f0=$row["company"];
        $f1=$row["car_moto"];
        $f2=$row["packet"];

        $sql_1 = "SELECT SUM(price_protect) as partpaid1 FROM companies where company='$f0' and car_moto='$f1' and packet='$f2' and userid='$userid'";
        $sql_2 = "SELECT SUM(clear_protect) as partpaid2 FROM companies where company='$f0' and car_moto='$f1' and packet='$f2' and userid='$userid'";

        $result_1=mysql_query($sql_1) or die('Error query failed');
        $result_2=mysql_query($sql_2) or die('Error query failed');

        $invoicepartpaid1=0;$invoicepartpaid2=0;
        while ($row_1 = mysql_fetch_array($result_1)) {
            $invoicepartpaid1 = $row_1['partpaid1'];
        }
        while ($row_2 = mysql_fetch_array($result_2)) {
            $invoicepartpaid2 = $row_2['partpaid2'];
        }
        echo "<tr><td align='center'><span>$f0</span></td><td align='center'><span>$f1</span></td><td align='center'><span>$f2</span></td>";
        echo "<td align='center'><span>$invoicepartpaid1</span></td><td align='center'><span>$invoicepartpaid2</span></td></tr>";
        $i++;
    }
?>