PHP - 在表中显示最后3个SQL行(不工作)

时间:2014-02-12 01:59:18

标签: php mysql sql

我正在尝试在我的网站上开发用户个人资料系统,其中包含用户提供的前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>

1 个答案:

答案 0 :(得分:0)

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