I know this probably will be silly, but I’m getting frustrated not finding the solution.
Here I go: I have a table which contains players, I would like to store the players into an array in a form like this:
Array ( [0] => player1 [1] => player2 [2] => player3)
now what I get from my code below is this:
Array ( [0] => Array ( [player] => player1 ) [1] => Array ( [player] => player2 ) [2] => Array ( [player] => player3 ))
So I get this extra layer into my array called ‘player’ which is the name of the column field I’m selecting from. How do I adjust my code?
$query_players="SELECT player FROM `whiteboard_games` WHERE id='$id'";
$result = mysql_query($query_players) or die;
$players = array();
while($row = mysql_fetch_assoc($result))
{ $players[] = $row; }
Thanks a lot!
There are many ways to handle this. If you have only the one column, rather than appending the whole
$row, the most straightforward is to just append the column you want:If you had the multidimensional array already and needed to flatten it, you can do so with a loop;
Note: We assume you have already filtered and validated
$idin your query, to protect against SQL injection.