First we request a list of tag identifiers for the file, then it is necessary to get the names of the tags themselves using these identifiers, then print the names on the page. Unfortunately, besides PHP, I didn’t understand other necessary languages, but it seems to me that this can be done with one request, tell me what to operate on?

$fileLabels = []; // запрашиваем перечень меток для текущего файла $query = " SELECT label_id FROM filelabels WHERE file_id = " . $_GET['id']; if ($result = $mysqli->query($query)) { while ($filelabels = $result->fetch_row()[0]) { $labelIDs[] = $filelabels; } // запрашиваем данные о метках (имена меток) для файла $query = " SELECT id, name FROM labels WHERE id IN (" . implode(", ", $labelIDs) . ") "; if ($result = $mysqli->query($query)) { while ($labels = $result->fetch_row()) { $fileLabels[$labels[0]] = $labels[1]; } } else echo $mysqli->error; } else echo $mysqli->error; 
  • one
    The question has already been asked many times. Look here for the inner join (i.e. on ru.so) - you need it. Roughly, it goes like this: SELECT labels.id, labels.name FROM labels INNER JOIN filelabels ON filelabels.label_id = labels.id WHERE filelabels.file_id = YOUR_FILE_ID - BOPOH

1 answer 1

Yes you can. This operation is called JOIN in SQL ( described in the MySQL documentation ), it has several types (on which it depends on which data is included in the sample), but, in a nutshell, it simply combines two tables:

  SELECT l.id, l.name FROM labels AS l INNER JOIN filelabels AS fl ON l.id = fl.label_id WHERE fl.file_id = :file_id 

In this case, a sample will be made, combining the rows of the tables labels and filelabels by matching labels.id and filelabels.label_id , after which the resulting sample will be filtered by filelables.file_id .