In article's page of my forum, program have to show up all article's comments. I use function-iterator that returns one entry from database (using PDO methods) after each iteration in foreach-cycle. It actually works and all required comments become visible. But article's page loads toooo slow, about 15 seconds. Also in console I see the Warning:
Unchecked runtime.lastError: The message port closed before a response was received. article.php:1
The questions are:
1. Why it works so slow & how to fix it?
2. Can the problem be caused by my weak laptop? I run all scripts on my localhost.
<?php
function getComment($article_id, $parent) {
if ($parent == 0) {
$comments = getPDO()->prepare("SELECT * FROM comments WHERE article_id = :article_id ORDER BY put_date DESC");
$comments->execute([
"article_id" => $article_id
]);
}
else {
$comments = getPDO()->prepare("SELECT * FROM comments WHERE article_id = :article_id AND parent = :parent ORDER BY put_date ASC");
$comments->execute([
"article_id" => $article_id,
"parent" => $parent
]);
}
while ($line = $comments->fetch(PDO::FETCH_ASSOC)) {
yield $line;
if(entryExists(getPDO(), $line["id"], "parent", "comments")) getComment(false, $line["id"]);
}
}
?>
<!-- some html... -->
<div class="comments_block">
<? foreach (getComment($_GET["id"], 0) as $line) { ?>
<div class="comment">
<!-- there is all stuff that uses info from $line -->
</div>
<? } ?>
</div>
<!-- some html... -->
entryExists() is the function that check existing of an entry;
getPDO() just returns a PDO-link to the database