简体   繁体   English

如何获取上一篇文章的用户名?

[英]How Can I Get The Username of The Last Post?

I am in the process of making my own forum and I wanted to be able to display the username of the last person to post in a topic.我正在制作自己的论坛,我希望能够显示最后一个在主题中发帖的人的用户名。 Below is the code I have got right now but I've tried changing the SQL functions and can't seem to get it right.下面是我现在得到的代码,但我尝试更改 SQL 函数,但似乎无法正确执行。 It will only display the same username in each row of the table.它只会在表格的每一行中显示相同的用户名。

Below is all the relevant code for it to display what I want.下面是它显示我想要的所有相关代码。

$host = "host"; // Host name 
$username = "username"; // Mysql username 
$password = "password"; // Mysql password 
$db_name = "db"; // Database name 
$tbl_name = "fquestions"; // Table name 
$tbl_name2 = "fanswer"; // Table name 

// Connect to server and select databse.
$conn = mysqli_connect($host, $username, $password)or die("cannot connect"); 
mysqli_select_db($conn, $db_name)or die("cannot select DB");

<table width="100%" style="font-size: 20px;">
    <tr>
    <td align="center" width="20%"><h4>Topic</h4></td>
    <td align="center" width="20%"><h4>Posted By</h4></td>
    <td align="center" width="20%"><h4>Replies</h4></td>
    <td align="center" width="20%"><h4>Last Post</h4></td>
    <td align="center" width="20%"><h4>Date / Time</h4></td>
    </tr>

<?php

// Start looping table row
$sql2 = "SELECT * FROM $tbl_name";
$result2 = mysqli_query($conn, $sql2);

if (mysqli_num_rows($result2) > 0) {
    while($rows = mysqli_fetch_array($result2)) {
        $topic = strtolower(htmlentities($rows['topic']));
        $topic = str_replace(get_html_translation_table(), "-", $topic);
        $topic = str_replace(" ", "-", $topic);
        $topic = preg_replace("/[-]+/i", "-", $topic);
        ?>
        <tr>
            <td align="center"><a href="view-topic?id=<?php echo $rows['id']; ?>/<?echo $topic ?>"><?php echo $rows['topic']; ?></a><br></td>
            <td align="center"><?php echo $rows['username']; ?></td>
            <td align="center"><?php echo $rows['reply']; ?></td>
            <?php
            $sql3 = "SELECT * FROM $tbl_name2 ORDER BY a_id DESC, LIMIT 1";
            $result3 = mysqli_query($conn, $sql3);

            while($rows2 = mysqli_fetch_array($result3)) {
                ?>
                <td align="center"><?php echo $rows2['a_username']; ?></td>
                <?php
            }
            ?>
            <td align="center"><?php echo $rows['datetime']; ?></td>
        </tr>

        <?php
    // Exit looping and close connection 
    }
} else {
    ?>
    <tr>
        <td align="center"><?php echo "0 records"; ?></td>
        <td align="center"><?php echo "0 records"; ?></td>
        <td align="center"><?php echo "0 records"; ?></td>
        <td align="center"><?php echo "0 records"; ?></td>
        <td align="center"><?php echo "0 records"; ?></td>
    </tr>
    <?php
}
?>
</table>

I have also added images of the table structures if that helps!如果有帮助,我还添加了表格结构的图像!

问题表结构

fanswer表结构

The fquestions table is where all topic titles and details are kept along with things like how many times they were viewed, who posted it etc and the fanswer one is where all answers are stored, the question_id is the same id as the one on the first table. fquestions 表是保存所有主题标题和详细信息以及查看次数、发布者等内容的位置,fanswer 表是存储所有答案的位置, question_id 与第一个的 ID 相同桌子。 The a_id is reply number and then the detail and username of who posted and also the date and time. a_id 是回复编号,然后是发布者的详细信息和用户名以及日期和时间。

In the loop, you are selecting the last row of fanswer , regardless whether it is related to your question / answer or not:在循环中,您选择fanswer的最后一行,无论它是否与您的问题/答案有关:

$sql3 = "SELECT * FROM $tbl_name2 ORDER BY a_id DESC, LIMIT 1";

You want something like (depending on your table structures and relations):你想要类似的东西(取决于你的表结构和关系):

$sql3 = "SELECT * FROM $tbl_name2 WHERE question_id = ? ORDER BY a_id DESC LIMIT 1";

or:或者:

$sql3 = "SELECT * FROM $tbl_name2 WHERE a_id = ? ORDER BY a_id DESC LIMIT 1";

where you bind the placeholder ?你在哪里绑定占位符? to the specific item you are in.到您所在的特定项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM