B
B
BlackWolf2018-03-03 00:14:07
PHP
BlackWolf, 2018-03-03 00:14:07

How to correctly select data from an array using the SELECT method?

The code with which I select the user's friends

$listfriends = 'SELECT friendid FROM `friends` WHERE userid="'.$id.'" and relation=1';
$listfriendssql = mysqli_query($connection, $listfriends) or die("Ошибка"); 
$data = [];
while($returnlist = mysqli_fetch_row($listfriendssql)) {
$data[] = $returnlist[0];

By writing for example Now I need to select a username which is equal to id from an array. I make a request:echo $data[1]; // выведется id юзера
$datasql = "SELECT name FROM `users` WHERE id=$data[0]"; // Из этого массива выводится имя, которое равно id из массива. То есть этот код нормально работает.
$querynameselect = mysqli_query($connection, $datasql) or die("Ошибка");
$data = mysqli_fetch_array($querynameselect);
echo $data[0];

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

I can’t understand why from the $data[0] array it shows a name equal to id, but it doesn’t work with arrays greater than 0 and gives an error. (Changed variable names. Didn't help.)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey, 2018-03-03
@blacknightwolf

It is necessary to call mysqli_fetch in a cycle, as is done in the first query.

S
Sergey, 2018-03-03
@Pendal

You are most likely overwriting the data array

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question