How to select data from the array to the SELECT method?

The code, which is selected user's friends
$listfriends = 'SELECT friendid FROM `friends` WHERE userid="'.$id.'" and relation=1';
$listfriendssql = mysqli_query($connection, $listfriends) or die("Error"); 
$data = [];
while($returnlist = mysqli_fetch_row($listfriendssql)) {
$data[] = $returnlist[0];

Writing, for example, echo $data[1]; // displays id of the user
Now I need to choose a username, which is equal id from the array. Make a request:
$datasql = "SELECT name FROM `users` WHERE id=$data[0]"; // this array lists the name, which is equal id from the array. That is, this code works fine.
$querynameselect = mysqli_query($connection, $datasql) or die("Error");
$data = mysqli_fetch_array($querynameselect);
echo $data[0];

But replacing $data[1], [2] etc code no longer works.
$datasql = "SELECT name FROM `users` WHERE id=$data[1]";

Can't understand why from the array $data[0] shows the name equal to the id, and the array is greater than 0 does not work and gives an error. (Variable names changed. It did not help.)
June 7th 19 at 14:37
3 answers
June 7th 19 at 14:39
Need mysqli_fetch call in a loop, as in the first query is done.
Why? The query that I stated in the question works successfully BUT it works only once, i.e., 1 friend he brings. But 2,3,4 etc anymore. Initially, all was so:
echo $data[0]; // led 1
//some html code 
echo $data[1]; // led 2

But now you just need to replace the id obtained on the names and the replacement only occurs once, as I have described in the question - madelynn commented on June 7th 19 at 14:42
see above, the answer changed - mona commented on June 7th 19 at 14:45
Yes, now the question is solved 50%.
$datasql = "SELECT name FROM `users` WHERE id=$data[0]";
$querynameselect = mysqli_query($connection, $datasql) or die("Error");
$friendsql = [];
while($friendsql = mysqli_fetch_row($querynameselect)) {
$friendname = $querynameselect;
echo $friendsql[0]; // now writes just 1 letter of the name.

Now change is happening, BUT only shows the first letter of the name.
Why did this happen? Thanks in advance for the answer! - madelynn commented on June 7th 19 at 14:48
inside the while loop write var_dump($friendsql); and it will be clear what to do next - mona commented on June 7th 19 at 14:51
Made. But nothing is clear. Here's what got:string(10) "Then the user's full name" - madelynn commented on June 7th 19 at 14:54
means echo $friendsql will display what you want. - mona commented on June 7th 19 at 14:57
echo $friendsql displays only this: string(10) "Then the user's full name"
But the function print_r before $friendname = $querynameselect helped. Now the question is solved. Thank you very much for your help and answers! :) - madelynn commented on June 7th 19 at 15:00
June 7th 19 at 14:41
You the data array is likely to fray
June 7th 19 at 14:43
$datasql = "SELECT `name` FROM `users` WHERE id='" . $vasahya_peremenaya . "';

Find more questions by tags PHPMySQL