所以我在mySQL中有一个表,其中包含具有customerID,productPrice和quantity的销售记录。我需要从每一行获得成本,然后将其编译为totalCost。我需要使用while循环(赋值指令)才能这样做,到目前为止我还没有运气。这是我现在的位置,感谢任何帮助
while ($record = mysqli_fetch_array($recordSet, MYSQLI_ASSOC)) {
$cost = $productPrice * $quantity
$totalCost = $totalCost + $cost
};
echo "$totalCost";
答案 0 :(得分:1)
$totalCost = 0;
while ($record = mysqli_fetch_array($recordSet, MYSQLI_ASSOC)) {
$cost = $record["productPrice"] * $record["quantity"];
$totalCost += $cost;
}
echo "$totalCost";
答案 1 :(得分:1)
这更像是一个注释,但是写一个不够酷,尝试将$ totalcost变量放在while循环的一边,这样它的值就不会被每次迭代覆盖。
$totalcost=0;
while ($record = mysqli_fetch_array($recordSet, MYSQLI_ASSOC)) {
$cost = $record['productPrice'] * $record['quantity'];
$totalCost = $totalCost + $cost;
};
答案 2 :(得分:0)
$totalCost = 0;
while ($record = mysqli_fetch_array($recordSet, MYSQLI_ASSOC)) {
$cost = $record['productPrice'] * $record['quantity'];
$totalCost += $cost
}
echo "$totalCost";
您首先需要声明$totalCost
,否则您将收到一个未定义的错误,在结束时不需要将大冒号添加到大括号中,并且当您使用{提取信息时{1}}您需要以数组键的形式访问信息,因此价格为$record = mysqli_fetch_array($recordSet, MYSQLI_ASSOC)
答案 3 :(得分:-1)
更改
$cost = $productPrice * $quantity
到
$cost = $record["productPrice"] * $record["quantity"];