dpc57092 2019-08-06 08:14
浏览 174

在PHP中使用PDO的yield-generator工作缓慢

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据