How can you place query results into an arbitrarily large array?

Viewed 32

I have a site with friends list, and such. I use while loops to iterate through the lists, and I wanted to use a foreach loop instead.

The while loop works just as it should there's no problem with it. I'm not asking how to use a foreach loop. But when I tried changing the while loop I realized the following code;

   <?php

$my_id = $_SESSION['user_id'];
$request = "SELECT `request_from` FROM `friend_requests` WHERE `request_to`='$my_id' ";
$request_query = mysqli_query($conn, $request);
while ($run_request = mysqli_fetch_array($request_query)){
$from = $run_request['request_from'];
$username = getuser($from, 'username');

echo"<table>";
echo "<tr><td>".$username."</td></tr>";
echo "</table>";
echo "<br />";
}


output: 

sammy davis jr
frank sinatra
dean martin

Doesn't produce AN array the contains friends, it contains an arbitrary number of arrays, that have only one element in them. As when I do

print_r($runrequest);

output:

Array ( [0] => 20 [request_from] => 20 ) 
Array ( [0] => 21 [request_from] => 21 ) 
Array ( [0] => 19 [request_from] => 19 ) 

So my question is, is there a way for me to rewrite my mysqli_query so that I produce only one, arbitrarily large, array that I would then iterate through with a foreach loop. As opposed to the query I have which produces an arbitrary number of arrays, that I have to iterate through with a while loop.

1 Answers
Related