Answer the question
In order to leave comments, you need to log in
How to organize the right requests?
The question is related to a mysql query.
There is a list of "articles" displayed on the page using while($row = $query -> fetch())
A problem is that you need to display not only the articles themselves, but also
while($row = $stmt -> fetch()){
$query = $DB->prepare("SELECT `login`,`gender`,`email` FROM `users` WHERE `id`=:author limit 1");
$query -> bindParam(':author', $row['author']);
$query -> execute();
$T2 = $query -> fetch();
$query = $DB->prepare("SELECT `id`,`time`,`name` FROM `comments` WHERE `comTo`=:id ORDER BY `id` DESC limit 1");
$query -> bindParam(':id', $row['id']);
$query -> execute();
$T = $query -> fetch();
// Допустим так выводит
echo "Название <a href="site.com/topic/'.$row['id'].'">".$row['name'].'</a>';
echo '<br> Автор <a href="'.$row['author'].'">'.$row[''].'</a>';
// и т.д.
} // end while
echo
and even more so ... Answer the question
In order to leave comments, you need to log in
Even without JOIN (you never know, some data can be multi-line) it is absolutely not necessary to fence loops.
You requested articles. Received data. Why withdraw immediately? Collect them for now in an array, along the way creating an array of id articles, authors, what else is needed there.
Next, make one (!) query - SELECT author_name FROM authors WHERE author_id IN (we already have this array). A similar query or two to the table with comments - and that's it, you don't need to pull the database anymore, and all the data for output has been received.
You see, little by little the realization will come of what benefits the separation of logic and conclusion brings ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question