PHP查询不返回所有结果

时间:2016-12-14 12:06:49

标签: php mysql

我试图显示用户制作的日期,相册和评论表。日期和评论显示没有问题,但目前我的查询只显示列表中的第一个CD标题。

表' cdreview'有列' CDID','用户ID',' reviewDate',' reviewText'。 表' cd'有CDTitle',所以我使用自然的JOIN通过CDID链接2个表,但我无法显示正确的CDTitle。

任何帮助都会非常感激。

<?php

    require_once 'database_conn.php';

        $userid = $_SESSION['userSession'];

$sql = "SELECT * FROM cdreview JOIN cd WHERE '$userid'=cdreview.userID ORDER BY reviewDate ASC";

$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));


$row = mysqli_fetch_assoc($result);

    $date = $row['reviewDate'];
    $album = $row['CDTitle'];
    $review = $row['reviewText'];
    $cdid = $row['CDID'];

?>

    <tr align='center'>
        <td><?php echo $date;?></td>
            <td><?php echo $album;?></td>
                <td><?php echo $review;?></td>
                    <td><a href="edit_review.php?id=<?php echo $cdid;?>">Edit</td>
                        <td><a href="album.php?id=<?php echo $cdid;?>">Delete</td>
    </tr>


</table>

2 个答案:

答案 0 :(得分:1)

你必须遍历结果:

while ($row = mysqli_fetch_assoc($result) ){

    $date = $row['reviewDate'];
    $album = $row['CDTitle'];
    $review = $row['reviewText'];
    $cdid = $row['CDID'];

    // print stuff
}

答案 1 :(得分:1)

mysqli_fetch_assoc正在返回result set

您必须遍历此结果集并单独处理每个结果。

while ($row = mysqli_fetch_assoc($result)) {
    $date = $row['reviewDate'];
    $album = $row['CDTitle'];
    $review = $row['reviewText'];
    $cdid = $row['CDID'];
    ?>
    <tr align='center'>
        <td><?php echo $date; ?></td>
        <td><?php echo $album; ?></td>
        <td><?php echo $review; ?></td>
        <td><a href="edit_review.php?id=<?php echo $cdid; ?>">Edit</td>
        <td><a href="album.php?id=<?php echo $cdid; ?>">Delete</td>
    </tr>
    <?php
}