php - 使 phpbb mysql 查询显示主题中的所有帖子

标签 php mysql phpbb

...与目前正在执行的操作相反,即显示指定论坛中每个主题的最后回复。

<?php
// How Many Topics you want to display?
$topicnumber = 10;
// Change this to your phpBB path
$urlPath = "../path/to/forum";

// Database Configuration (Where your phpBB config.php file is located)
include '../path/to/forum/config.php';

$table_topics = $table_prefix. "topics";
$table_forums = $table_prefix. "forums";
$table_posts = $table_prefix. "posts";
$table_users = $table_prefix. "users";
$link = mysql_connect("$dbhost", "$dbuser", "$dbpasswd") or die("Could not connect");
mysql_select_db("$dbname") or die("Could not select database");

$query = "SELECT t.topic_id, p.post_text, t.topic_title, t.topic_last_post_id, t.forum_id, p.post_id, p.poster_id, p.post_time, u.user_id, u.username
FROM $table_topics t, $table_forums f, $table_posts p, $table_users u
WHERE t.topic_id = 7 AND
f.forum_id = t.forum_id AND
t.forum_id = 11 AND
t.topic_status <> 2 AND
p.post_id = t.topic_last_post_id AND
p.poster_id = u.user_id
ORDER BY p.post_id DESC LIMIT $topicnumber";
$result = mysql_query($query) or die("Query failed");                                   

print '<div class="news_block">';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {


echo  "<h4><a href=\"$urlPath/viewtopic.php?f=$row[forum_id]&t=$row[topic_id]&p=$row[post_id]#p$row[post_id]\" TARGET=\"\">" .
$row["topic_title"] .
"</a> </h4>By: " .
$row["username"] . ' - ' . date("l", $row["post_time"]) .
"<p>" .
$row["post_text"] . // <----- 
"</p>";
}
print "</div>";
mysql_free_result($result);
mysql_close($link);

/* date("l", $row["post_time"]) .  date('F j, Y, g:i a', $row["post_time"]) .*/
?>

正如您可能已经知道的那样,我距离编码员还很远。我想我已经将范围缩小到需要更改的 p.post_id ,但是无论我似乎分配给它什么整数或变量,我都无法获得所需的效果。此时非常感谢一些帮助。谢谢。

最佳答案

我认为您必须从查询中删除这部分:

AND p.post_id = t.topic_last_post_id 

现在您正在请求与该主题的最后一篇帖子具有相同 id 的帖子(只有一篇),但您不想只获取最后一篇。您想获得所有帖子。

编辑: 您必须查看数据库并检查帖子表中是否有引用该主题的外键。

这样想:每个论坛都有很多主题,每个主题都有很多帖子,每个帖子只有一个用户。因此,您必须在表之间建立一个连接,您可以遵循该连接来确定行如何匹配在一起。

联接在这方面非常有帮助。如果您使用已包含这些外键的完成的数据库架构,联接将帮助您连接查询。

这将是一个格式良好的查询,支持我的解释。

SELECT 
    *
FROM
    $table_forums f
        LEFT JOIN
    $table_topics t ON t.forum_id = f.forum_id
        LEFT JOIN
    $table_posts p ON t.topic_id = p.topic_id
        LEFT JOIN
    $table_users u ON p.poster_id = u.user_id
WHERE
    t.topic_id = 7 AND t.forum_id = 11
        AND t.topic_status <> 2
ORDER BY p.post_id DESC
LIMIT $TOPICNUMBER

关于php - 使 phpbb mysql 查询显示主题中的所有帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48321247/

相关文章:

php - mysql_connect()打开的数据库连接如何关闭?

mysql - 使用 select 更新查询——它如何知道要更新哪一行

php - MySQL/PHP 检查登录详细信息

php - 插入带有 Select 和附加参数的 stmt

c# - 通过 C# 应用程序在 phpBB 版 block 中发帖

php - 如何根据用户交互对结果进行排序

javascript - 如何使用 php-phantomjs 在 headless chrome 中执行 Javascript?

php - 2层多维数组如何变成n层?

PHPbb DBAL 拒绝执行正确的 PHP 和 SQL

phpBB 自动向所有成员(member)发送有关所有帖子的电子邮件通知