我需要一些幫助來修復一些我有的代碼。PHP MySQL列總數
我有一個查詢,從表中抽出一些行,但我需要它來在其中一列'價格'的總價值。
我得到了總計算工作正常和輸出,但它似乎打破我的重複區域進一步向下頁面。我認爲這與WHILE聲明有關,但我不完全確定如何解決它。任何人都可以提出一種解決方法或提供替代方法。
mysql_select_db($database_otters, $otters);
$query_CartDetails = "SELECT eventregistrations.*, swimmers.*, events.*, eventtypes.*, timetable.* FROM eventregistrations LEFT JOIN swimmers ON eventregistrations.SwimmerID=swimmers.SwimmerID LEFT JOIN events ON eventregistrations.EventID=events.EventID JOIN eventtypes ON events.EventTypeID=eventtypes.EventTypeID JOIN timetable ON eventregistrations.ParentDiaryID=timetable.DiaryID WHERE eventregistrations.SessionID = '$sessionid'";
$CartDetails = mysql_query($query_CartDetails, $otters) or die(mysql_error());
$row_CartDetails = mysql_fetch_assoc($CartDetails);
$totalRows_CartDetails = mysql_num_rows($CartDetails);
$cartvalue= 0;
while ($num = mysql_fetch_assoc($CartDetails)) {
$cartvalue += $num['Price'];
}
這是因爲如果我註釋掉上面的「cartvalue」部分工作正常重複區域代碼:
<?php do { ?>
<tr>
<td><?php echo $row_CartDetails['Fixture']; ?></td>
<td><?php echo $row_CartDetails['EventType']; ?></td>
<td><?php echo $row_CartDetails['SwimmerFName']; ?> <?php echo $row_CartDetails['SwimmerSName']; ?></td>
<td>£<?php echo $row_CartDetails['EventCost']; ?></td>
</tr>
<?php } while ($row_CartDetails = mysql_fetch_assoc($CartDetails)); ?>
mysql已棄用。你應該遷移到像pdo這樣的東西。只是說。 – Gogol
在「重複區域」之前,您已經遍歷了一組「mysql_fetch_assoc($ CartDetails)」,因此當您再次使用它時,它無法再返回。 – ljacqu
如果工作不正常,你會得到什麼? – EmCo