0

I am attempting to loop through the rows of a database (in this case players) and gather their scores. The columns of the database represent different positions, for each of which a player has a score value. As there are more columns than positions within a formation, the query must SELECT only those 11 columns within the given formation ($most_common_formation). The scores must then be multiplied by the corresponding value in the player's "total_picks" column before being placed into a multidimensional array.

The final array should contain the 25 players as arrays, and within each display the "full_name" along with the 11 scores that have been multiplied by "total_picks".

The $team array holds the "full_names" of all players.

Please help it's driving me crazy!

// 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);


}
  • 3
    Can't use ? as placeholder for a select (SELECT name,age), only in a WHERE name=?, age=? – clearshot66 Jun 29 '17 at 13:28
  • Why cant I do that I hear you ask! Well because a `prepare` sends the query to the database, where it is compiled and optimised etc. Now if the database does not know which columns you want to retrieve.. A) It cannot compile it and B) It definitely cannot optimise it and produce a plan – RiggsFolly Jun 29 '17 at 13:52

0 Answers0