无法使用pdo获取行ID

时间:2015-04-12 02:13:27

标签: php mysql pdo

我无法使用PDO获取我的数据库行。目前我正在使用fetch(PDO::FETCH_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循环之后我无法获取我的行[&#39; topicsubject&#39;]我错过了什么。任何人都可以帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

对于一个小问题哇哇这么多辩论。您第一次调用fetchColumn()从唯一的行中获取数据。因此,fetchAll()无法获取任何内容。