PHP - 从MySQL数据库中提取数据并放入多维数组

时间:2017-06-29 13:27:40

标签: php mysql arrays

我试图遍历数据库的行(在这种情况下是玩家)并收集他们的分数。数据库的列表示不同的位置,对于每个位置,玩家具有得分值。由于列中的列数多于列中的位置,因此查询必须仅选择给定格式中的11列($ most_common_formation)。然后必须将分数乘以玩家的“total_picks”列中的相应值,然后才能将其放入多维数组中。

最终数组应包含25个玩家作为数组,并在每个显示中包含“full_name”以及已经乘以“total_picks”的11个分数。

$ team数组保存所有玩家的“full_names”。

请帮助它让我发疯!

// Create array to hold final scores per player
$player_scores = array();

// Loop through all players in $team array
for ($l=0; $l < 25; $l++) {

    // SELECT each players scores per position in $most_common_formation
    $players_picks_query = "SELECT 
                                ?,
                                total_picks,
                                ?,
                                ?,
                                ?,
                                ?,
                                ?,
                                ?,
                                ?,
                                ?,
                                ?,
                                ?,
                                ?
                            FROM table";

    // prepare and bind statement
    $stmt = mysqli_prepare($conn, $players_picks_query) or die(mysqli_error($conn));
    mysqli_stmt_bind_param($stmt, 'ssssssssssss', $player_name, $pos1, $pos2, $pos3, $pos4, $pos5, $pos6, $pos7, $pos8, $pos9, $pos10, $pos11);

    $player_name = $team[$l];
    $pos1 = $most_common_formation[0];
    $pos2 = $most_common_formation[1];
    $pos3 = $most_common_formation[2];
    $pos4 = $most_common_formation[3];
    $pos5 = $most_common_formation[4];
    $pos6 = $most_common_formation[5];
    $pos7 = $most_common_formation[6];
    $pos8 = $most_common_formation[7];
    $pos9 = $most_common_formation[8];
    $pos10 = $most_common_formation[9];
    $pos11 = $most_common_formation[10];

    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);

    while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
        foreach ($row as $r) {
            echo "$r ";
        }
        echo "<br>";
    }

    // close statement
    mysqli_stmt_close($stmt);


}

0 个答案:

没有答案