从特定行获取求和值

时间:2013-11-24 15:51:52

标签: php mysql rows

我有这个:

$result = mysqli_query($mysqli, "SELECT balance FROM " . MYSQLBTCTABLE . " WHERE address='" . $_POST['address'] . "' LIMIT 1");
while($row = mysqli_fetch_assoc($result)) {
    $balance = $row['balance'];

echo "<font style='font-weight:bold;'>Your current balance is: </font><br/>";
printf("%0.8f", ($balance / 100000000));

但它只获取我需要的最后一个值来查找该地址的所有余额,并将$balance的总和相加。

5 个答案:

答案 0 :(得分:0)

使用

$balance += $row['balance'];

答案 1 :(得分:0)

使用agregation:

$result = mysqli_query($mysqli, "SELECT sum(balance) FROM " . MYSQLBTCTABLE . " WHERE address='" . $_POST['address'] . "' GROUP BY address");

答案 2 :(得分:0)

从查询中删除LIMIT并添加SUM

$result = mysqli_query($mysqli, "SELECT SUM(balance) as totalBalance FROM " . MYSQLBTCTABLE . " WHERE address='" . $_POST['address'] . "'");
$row = mysqli_fetch_assoc($result);
$balance = $row['totalBalance'];

echo "<font style='font-weight:bold;'>Your current balance is: </font><br/>";
printf("%0.8f", ($balance / 100000000));

答案 3 :(得分:0)

首先,改为:

$balance = $row['balance'];

使用:

$balance += $row['balance'];

此外,摆脱“LIMIT 1”约束

答案 4 :(得分:0)

Remove limit 1 and do the sum

        $result = mysqli_query($mysqli, "SELECT balance FROM " . MYSQLBTCTABLE . " WHERE      
        address='" . $_POST['address'] . "' ");
        $balance = 0;
        while($row = mysqli_fetch_assoc($result)) {
           $balance += $row['balance'];
        echo "<font style='font-weight:bold;'>Your current balance is: </font><br/>";
        printf("%0.8f", ($balance / 100000000));