我怎样才能获得最后帖子的用户名?

时间:2018-03-05 19:43:00

标签: php sql

我正在制作自己的论坛,我希望能够在主题中显示最后一个人的用户名。下面是我现在获得的代码,但我已经尝试过更改SQL函数,但似乎无法正确完成。它只会在表格的每一行显示相同的用户名。

以下是显示我想要的所有相关代码。

$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>

如果有帮助,我还添加了表结构的图像!

fquestions table structure

fanswer table structure

fquestions表是所有主题标题和详细信息一起保存的地方,例如查看了多少次,发布了哪些内容等等,而fanfan是存储所有答案的地方,question_id与一个相同的id在第一张桌子上。 a_id是回复号码,然后是发布者的详细信息和用户名以及日期和时间。

1 个答案:

答案 0 :(得分:1)

在循环中,您选择fanswer的最后一行,无论它是否与您的问题/答案相关:

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

你想要的东西(取决于你的表结构和关系):

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

或:

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

将占位符?绑定到您所在的特定项目。