数据库查询(MySQL with PHP)没有出现在DOM中

时间:2016-02-16 22:39:07

标签: php mysql database

我试图在MySQL中查询多对多关系,使用连接表来显示哪些"工作空间"有多少"成员"订阅了它。

查询本身正常工作,在PHPMyAdmin中进行了测试和测试,并返回了预期的结果。然而。在实际的PHP代码中使用此查询并执行它时,循环中的用于重建为DOM 准备好的结果! 我甚至放了一点回声来查看服务器是否正在执行该循环。看来不是。好像查询检索到零结果,即使在循环之前的var_dump显示实际上已从服务器接收数据。

我不是这个程序的新手,我已成功完成多次,但这个人真的不喜欢我。

为什么这不会出现?

配置代码

try {

    // Create PDO connection
    $db = new PDO("mysql:host=" . DB_HOST . "; dbname=" . DB_NAME, DB_USER, DB_PW);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch(PDOException $e) {
    // Show Error
    echo '<p class="bg-danger">' . $e->getMessage() . '</p>';
    exit;
};
?>

PHP代码:

        <?php
            // Show and list threads
            try {
                // Prepare and gather Threads from DB
                $stmt = $db->prepare('SELECT work.*, sub.*, mem.username, mem.avatar FROM TEAM_workspaces AS work LEFT JOIN TEAM_subscriptions_JCN AS sub ON work.workspaceID = sub.workspacesID LEFT JOIN TEAM_members AS mem ON sub.memberID = mem.memberID WHERE mem.memberID = :memberID');
                $stmt->execute(array(':memberID' => $_SESSION['memberID']));

                // This Block is Debugging Code to see what is happening
                echo '<br><br>';
                var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
                echo '<br><br>';
                $row = $stmt->fetchAll(PDO::FETCH_ASSOC);
                var_dump($row);

                // Cycle Database Records for Subscribed Workspaces
                while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                    echo 'Hello, is this even being Parsed??'; // Added to see if the Loop is being executed at all
                    echo '<div class="row"><div class="col-xs-12 col-sm-12 col-md-12"><a href="workspace-single.php?workspaceID=' . $row['workspaceID'] . '">';
                    echo '<div class="col-md-10"><h2>' . $row['title'] . '</h2></div>';
                    echo '</a></div></div>';
                }
            } catch(PDOException $e) {
                echo '<p class="bg-danger">' . $e->getMessage() . '</p>';
            }
            ?>

渲染内容的屏幕截图:

Screenshot of Rendered PHP

数据库的结果(更易于阅读)

Easier to Read MySQL Query Completed within PHPMyAdmin

程序代码的原始(工作)版本

作为一个开始的基础,我复制了另一个类似项目的代码。我已粘贴下面的内容,以提供更多关于情况的图片。

来自另一个项目的(WORKING)PHP代码

                <?php
                // Show and list threads
                try {
                    // Prepare and gather Threads from DB
                    $stmt = $db->prepare('SELECT threadID, title, description, author, date, imagepath FROM FORUM_threads');
                    $stmt->execute();
                    // $rows = $stmt->fetch(PDO::FETCH_NUM);

                    if( isset($row['imagepath']) ) {
                        $imagepath = $row['imagepath'];
                    } else {
                        $imagepath = 'default.bmp';
                    }

                    // Cycle Database Records
                    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                        echo '<div class="row"><div class="col-xs-12 col-sm-12 col-md-12"><a href="thread.php?threadID=' . $row['threadID'] . '">';
                        echo '<div class="col-md-2 col-sm-2 col-sx-4 pull-left"><img class="thread-image" src="uploads/' . $imagepath . '" alt="Avatar for the thread ' . $row['title'] . '" /></div>';
                        echo '<div class="col-md-10 col-sm-10 col-sx-12"><h2>' . $row['title'] . '</h2></div></a>';
                        echo '<div class="col-md-10 col-sm-10 col-sx-12">' . $row['date'] . '</div>';
                        echo '<div class="col-md-10 col-sm-10 col-sx-12">' . $row['description'] . '</div>';
                        echo '<div class="col-md-10 col-sm-10 col-sx-12"><a class="btn btn-primary btn-block btn-md" href="compose.php?type=message&threadID=' . $row['threadID'] . '">New Message</a></div>';
                        echo '</div></div>';
                    }
                } catch(PDOException $e) {
                    echo '<p class="bg-danger">' . $e->getMessage() . '</p>';
                }
                ?>

1 个答案:

答案 0 :(得分:2)

$stmt->fetchAll(PDO::FETCH_ASSOC)会占用您的整个结果集。

后续$row = $stmt->fetch(PDO::FETCH_ASSOC))操作尝试从已经消耗的结果集中获取另一行,因此不进行任何操作(就像结果集为空)。