0

我有很长的路要走最后一个问题。一切都是我的代码现在正在运行,但我无法解决最后一个小问题。现在我有:

$sql =  "SELECT   phonenumber,email, dataplan AS currentplan, SUM(datamb) AS 
value_sum FROM maindata GROUP BY phonenumber, dataplan";


$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$val = $row["value_sum"]; 
$plan = $row["currentplan"];
$remain = $plan - $val;


if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

它只减去第一个值,而不是所有值。显示如下:

while ($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$row['phonenumber'] . "</td> ";
echo "<td>".$row['currentplan'] . "</td> ";
echo "<td>".ROUND ($row["value_sum"],2) . "MB</td> ";
echo "<td>".$remain . " MB</td> ";
echo "<td>".$row['email'] . "</td></tr>"; 
} 

所以我的目标是从所有数据计划中减去所有 value_sums,但是我现在拥有的,给了我所有列的第一个值。谢谢!

4

1 回答 1

0

mysql_fetch_assoc()总是会得到一排。您可以在循环中使用它,或者更好地使用 PDO,例如。像这样:

$sql =  "SELECT   phonenumber,email, dataplan AS currentplan, SUM(datamb) AS 
         value_sum FROM maindata GROUP BY phonenumber, dataplan";
$results = $pdo->query($sql);

您可以在此处阅读有关创建PDO连接的信息http://www.php.net/manual/en/book.pdo.php

于 2013-10-08T16:03:04.887 回答