gpt4 book ai didi

PHP - 显示表中最后 3 行 SQL 行(不起作用)

转载 作者:行者123 更新时间:2023-11-30 00:41:47 25 4
gpt4 key购买 nike

我正在尝试在我的网站上开发一个用户个人资料系统,其中包含用户之前发布的 3 个帖子。我可以让它选择前 3 条记录,但它只会显示其中一条。我是不是因为凌晨 2 点就想编码而变得愚蠢?

<?php                       
$q = "SELECT * FROM blog_cmt WHERE uid=".$profile_uid." ORDER BY id DESC LIMIT 3";
$r = $db->query($q);
$c = $r->num_rows;
?>

<table class="table">
<?php
while($post = $r->fetch_assoc()) {
$pid = $post['pid'];
$q = "SELECT * FROM blog WHERE pid=".$pid;
$r = $db->query($q);
$blog = $r->fetch_assoc();
$title = $blog['title'];
$date = timeSince(strtotime($post['date']));
?>
<tr>
<td>
<a href="http://www.my_domain.co.uk/thread.php?id=<?php echo $pid; ?>">Commented on <?php echo $title; ?></a>
</td>
<td>
<?php echo $date; ?> ago
</td>
</tr>
<?php
}
?>
</table>

最佳答案

<?php

$profile_post_qry = "SELECT * FROM blog_cmt WHERE uid=".$profile_uid." ORDER BY id DESC LIMIT 3";
$profile_posts = $db->query($profile_post_qry);

$profile_post_count = $profile_posts->num_rows;

?>

<table class="table">

<?php

while($post = $profile_posts->fetch_assoc()) {

$pid = $post['pid'];
$blog_qry = "SELECT * FROM blog WHERE pid=".$pid;
$blog_info = $db->query($blog_qry);
$blog = $blog_info->fetch_assoc();
$title = $blog['title'];
$date = timeSince(strtotime($post['date']));

?>

<tr>
<td>
<a href="http://www.my_domain.co.uk/thread.php?id=<?php echo $pid; ?>">Commented on <?php echo $title; ?></a>
</td>
<td>
<?php echo $date; ?> ago
</td>
</tr>

<?php

关于PHP - 显示表中最后 3 行 SQL 行(不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21717121/

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