Cannot fetch row id using pdo [closed]

2020-05-09 01:59发布

问题:

I cant fetch my db row using PDO. Currently I am using fetch(PDO::FETCH_ASSOC) but my result is showing blank.

This is my code:

    <?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';
?>

After the while loop I cannot fetch my row['topicsubject'] did I missed something .Can anyone help me with this. Thank you.

回答1:

Wow so much debate for a small problem. Your first call to fetchColumn() fetches the data from the only row there is. So there is nothing left for fetchAll() to fetch.