祝你好运!
我已经花了好几个小时想弄明白这个问题了,但就是弄不好。
我有一个论坛,它应该显示所有的线程,按最新的回复排序。
数据库:
<表线程/em>
Thread_id, category_id, title, created, author, status
Reply_id, thread_id, created, text
查询:
$query = mysqli_query($link, "SELECT * FROM threads, replies
WHERE threads.thread_id=replies.thread_id AND threads.category_id=$cat
AND threads.status=1 ORDER BY replies.reply_id DESC");
问题:它只显示具有最新回复的线程。我使用while循环回显所有行。Mysqli_num_rows ($query)显示3,但只打印一个线程。while循环只包含:
while($r = mysqli_fetch_assoc($query))
{
$thread_id = $r['thread_id'];
$total_comments = mysqli_num_rows(mysqli_query($link, "SELECT thread_id FROM replies WHERE thread_id=$thread_id"));
$last_comment = mysqli_query($link, "SELECT * FROM replies WHERE thread_id=$thread_id ORDER BY reply_id DESC LIMIT 0,1");
$rLastComment = mysqli_fetch_assoc($last_comment);
$query = mysqli_query($link, "SELECT author FROM threads WHERE thread_id=$thread_id");
$a = mysqli_fetch_assoc($query);
echo '<tr>
<td><img src="" alt=""></td>
<td><a href="/forum/thread/'.$thread_id.'">'.$status.''.(substr($r['title'], 0, 15)).'</a> </td>
<td>'.$a['author'].'</td>
<td>'.($total_comments).'</td>
<td>'.$lastComment.'</td>
</tr>';
}
您需要重复调用mysqli_fetch_assoc()
,每行调用一次。
查看PHP文档中有关如何使用while循环输出结果的示例:http://php.net/manual/en/mysqli-result.fetch-assoc.php