我有這個表中MySQL數據庫:擷取來自在PHP MySQL查詢結果數組,而循環問題
SELECT score, count(*) FROM Coaches group by score ORDER BY score DESC
結果表如下所示:
運行此查詢後這樣的:
現在在PHP中,我嘗試獲取結果並遍歷數組,以確定每個教練屬於哪個組並獲得他在排名中的位置。因此我寫到:
$groupsOfScoresQuery = "SELECT score, count(*) FROM Coaches group by score ORDER BY score DESC";
$result = mysqli_query($dbc, $groupsOfScoresQuery);
if ($result) { // query did successfully run
$response['topCoaches'] = array();
if (mysqli_num_rows($result) > 0) {
while ($rowScore = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$currentRanking++;
$score = array(); // temp user array for one group of scores
$numberOfCoaches; // Number of coaches with this particular number of scores
$scoresGroup; // Scores in the particular group
$score["scores"] = $rowScore["score"];
$score["count"] = $rowScore["count(*)"];
$numberOfCoaches = $score["count"];
$scoresGroup = $score["scores"];
$response["scoresGroup"] = $scoresGroup; // HERE IS THE PROBLEM
.
.
.
more processing
} // end WHILE
爲什麼$response["scoresGroup"]
將始終conatins結果的最後一個值?在這種情況下,這是。我認爲這是通過循環的第一次迭代,並且第一個元素(474)將持有第一個元素,在第二次迭代期間應該保持382?我在這裏做錯了什麼?我是否使用正確的函數來獲取結果?還是應該用不同的循環來實現我的目標?我在這裏先向您的幫助表示感謝。
感謝您的答覆。所有的答案都是正確的+1我接受了最後一個。 –