Php 无法使用pdo获取行id

Php 无法使用pdo获取行id,php,mysql,pdo,Php,Mysql,Pdo,我无法使用PDO获取我的db行。目前我正在使用fetch(PDO::fetch\u ASSOC),但结果显示为空白 这是我的代码: <?php ini_set('display_errors', 1); //create_cat.php include 'dbfunctions.php'; include 'forum_header.php'; $db = getConnection(); $db->setAttribute(PDO::ATTR_ERRMODE, PDO:

我无法使用PDO获取我的db行。目前我正在使用
fetch(PDO::fetch\u ASSOC)
,但结果显示为空白

这是我的代码:

    <?php
ini_set('display_errors', 1);
//create_cat.php
include 'dbfunctions.php';
include 'forum_header.php';

$db = getConnection();

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$id = $_GET['id'];

$sql = "SELECT
            topicid,
            topicsubject
        FROM
            topics
        WHERE
            topics.topicid = :id ";

$result = $db->prepare($sql);

$result->bindParam(":id", $id, PDO::PARAM_INT);

$result->execute();

$numrows = $result->fetchColumn();


if(!$result)
{
    echo 'The topic could not be displayed, please try again later.';
}
else
{

    while($topicrow = $result->fetchAll(PDO::FETCH_ASSOC))
    {
            echo "hello";

        //display post data
        echo '<table class="topic" border="1">';
        echo '<tr>';
        echo '<th colspan="2">' . $topicrow['topicsubject'] . '</th>';
        echo '</tr>';
        //fetch the posts from the database
        $posts_sql = "SELECT
                    posts.topicid,
                    posts.postcontent,
                    posts.postdate,
                    posts.postby,
                    users.userID
                FROM
                    posts
                LEFT JOIN
                    users
                ON
                    posts.postby = users.userID
                WHERE
                    posts.topicid = :id ";

        $posts_result = $db->prepare($posts_sql);
        $posts_result->bindParam(":id", $id, PDO::PARAM_INT);
        $posts_result->execute();
        $posts_numrows = $posts_result->fetchColumn();

        if(!$posts_result)
        {
            echo '<tr><td>The posts could not be displayed, please try again later.</tr></td></table>';
        }
        else
        {

            while($posts_row = $posts_result->fetch(PDO::FETCH_ASSOC))
            {
                echo '<tr class="topic-post">
                        <td class="user-post">' . $posts_row['userID'] . '<br/>' . date('d-m-Y H:i', strtotime($posts_row['postdate'])) . '</td>
                        <td class="post-content">' . htmlentities(stripslashes($posts_row['postcontent'])) . '</td>
                      </tr>';
            }
        }

        if(!$_SESSION['CurrentUser'])
        {
            echo '<tr><td colspan=2>You must be <a href="signin.php">signed in</a> to reply. You can also <a href="signup.php">sign up</a> for an account.';
        }
        else
        {
            //show reply box
            echo '<tr><td colspan="2"><h2>Reply:</h2><br />
                <form method="post" action="forum_reply.php?id=' . $row['topicid'] . '">
                    <textarea name="reply-content"></textarea><br /><br />
                    <input type="submit" value="Submit reply" />
                </form></td></tr>';
        }

        //finish the table
        echo '</table>';
    }
}

include 'forum_footer.php';
?>


在while循环之后,我无法获取我的行['topicsubject'],我是否遗漏了什么。有人能帮我解决这个问题吗。谢谢。

一个小问题引起了这么多的争论。您对
fetchColumn()
的第一次调用将从仅有的行中获取数据。因此,没有任何内容可供
fetchAll()
提取。

运行此操作时,返回的具体内容是什么?它触发哪个if/else语句?如果没有,添加
ini\u集('display\u errors',1)显示到文件的最顶端,以显示任何可能隐藏的错误。旁注:如果您确实修复了此问题,并且您已经修复了,那么假设其中有10行?您将创建10个表标记。我怀疑你想要
echo'在该循环内。@Steve my code在第一条else语句和第二条else语句中执行,但在第二条else语句之后,它在while循环内不工作。我刚才做了一些测试。因此,一旦我将echo“test”放入while循环,就不会显示任何结果在连接打开后立即执行。假设您正在使用
$db
作为连接函数中的变量。另外,您发布的代码缺少几个大括号,或者是否相关。哦,我知道这里出了什么问题。你的条件语句太离谱了。您在这里所做的是第一次检查是否没有结果。然后您的
else
和其他嵌套的
if
s检查是否没有行等。请重新考虑这一点。