B
B
BlackWolf2018-02-23 21:28:27
PHP
BlackWolf, 2018-02-23 21:28:27

How to get data from database into array?

I retrieved the data from the database in the order I needed:

$friends = 'SELECT friend FROM `friends` WHERE id="'.$id.'" and value=1';
$listfriends = mysqli_query($connection, $friends) or die("Ошибка");
while($returnlist = mysqli_fetch_row($listfriends)) {
print $returnlist[0]; // Выводит 1, 2, 3 (только слитно)
}

But I need data in arrays. That is, so that I can display the received data at the right time and in any part of the page, but outside the print loop, echo does not work. For example, like this:
echo $returnlist[0]; // Чтобы вывел не 1, 2, 3, а 1.
echo $returnlist[1]; // Чтобы вывел 2.
echo $returnlist[2]; // Чтобы вывел 3.

Thanks in advance for your reply.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2018-02-23
@blacknightwolf

$data = [];
while($returnlist = mysqli_fetch_row($listfriends)) {
    $data[] = $returnlist[0];
}
echo $data[0];
echo $data[1];
...

M
Merzley, 2018-02-23
@Merzley

In short, you can do this:

$SQL = 'SELECT friend FROM `friends` WHERE id="'.$id.'" and value=1';
$requestResult = mysqli_query($connection, $SQL) or die("Ошибка");
$arFriends = [];
while($resultRow = mysqli_fetch_row($requestResult)) {
    $arFriends[] = $resultRow[0];
}
echo $arFriends[0]; //Должно вывести 1
echo $arFriends[1]; //Должно вывести 2
// и.т.д

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question