gpt4 book ai didi

php - 无法使用 pdo 获取行 ID

转载 作者:行者123 更新时间:2023-11-29 22:34:42 25 4
gpt4 key购买 nike

我无法使用 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 循环之后,我无法获取我的行['topicsubject'] 我是否错过了一些东西。任何人都可以帮我解决这个问题。谢谢。

最佳答案

哇,为了一个小问题有这么多争论。您第一次调用 fetchColumn() 会从唯一的行中获取数据。因此,没有任何东西可供 fetchAll() 获取。

关于php - 无法使用 pdo 获取行 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29586042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com