无法使用PDO获取行ID [英] Cannot fetch row id using pdo
本文介绍了无法使用PDO获取行ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法使用PDO提取数据库行.目前,我正在使用fetch(PDO::FETCH_ASSOC)
,但是我的结果显示为空白.
I cant fetch my db row using PDO. Currently I am using fetch(PDO::FETCH_ASSOC)
but my result is showing blank.
这是我的代码:
<?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'],我是否错过了某些东西.有人可以帮助我吗?谢谢.
After the while loop I cannot fetch my row['topicsubject'] did I missed something .Can anyone help me with this. Thank you.
推荐答案
对于一个小问题,辩论如此之多.您对fetchColumn()
的第一次调用从仅有的一行中获取数据.因此,fetchAll()
不需要提取任何内容.
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.
这篇关于无法使用PDO获取行ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文