简体   繁体   中英

PHP array has the same index for every element

I'm working on a program that prints the qualities of every individual in a team.

I have 3 tables: The Teams table:

TeamID
1
2

The TeamPlayers table:

PlayerID Team
100 1
269 1
357 2

The program works like this: There is a textbox in the HTML page where the user is typing the ID of the team and the program outputs the members. The problem is that each player is stored in an array. and all have the same index:

$fetchPlayersSQL = "SELECT Player_ID
            FROM TeamPlayers
            WHERE Team = $TeamID;";
$fetchPlayers = ibase_query($dbConnection, $fetchPlayersSQL);
while ($row2 = ibase_fetch_object($fetchPlayers)) {
    $fetchPlayersArray = get_object_vars($row2);
    print_r($fetchPlayersArray);
}

Keep in mind $TeamID is the value introduced by the user in the HTML textbox.

Now, this program outputs this:

Array ( [Player_ID] => 2157 ) 

Array ( [Player_ID] => 734 ) 

Array ( [Player_ID] => 2160 ) 

Array ( [Player_ID] => 3744 ) 

Array ( [Player_ID] => 2166 )

(Keep in mind, 2157, 734, 2160, 3744 and 2166 are other players, there are a lot of them, but I listed only a few)

The problem is that I need every player to have their own index in the array, because I have to print their qualities

I really can't find where the problem comes from. Maybe I am using an incorrect method to select since there are more players in a team.

The 3rd table is just the qualities of every player

PlayerID Height Weight HairColor
100 187 80 black
357 167 67 grey
269 182 95 brown

And the expected output is something like this:

The user enters 1 in the HTML textbox, Team 1 players are 100 and 269 so they should see:

Array ( [0] => 2157 )

Array ( [1] => 734  )

echo $row2->Player_ID; just prints their IDs, we have player 100 and 269 in the Team 1, and this prints 100269

It might not be the most elegant solution, but you can always build that array yourself in a loop:

$a = array();
while ($row2 = ibase_fetch_object($fetchPlayers)) {
    $a[] = $row2->Player_ID;
}
print_r($a);

See if that works for you, I am not able to code at the moment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM