2

I'm trying to display a table of dates, albums and reviews made by users. The date and review show up with no problem, but at the moment my query is only displaying the first CD Title in the list.

Table 'cdreview' has columns 'CDID', 'userID', 'reviewDate', 'reviewText'. Table 'cd' has 'CDTitle', so I've used a natural JOIN to link the 2 tables by the CDID, but I can't display the correct CDTitle.

Any help would be extremely grateful.

<?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
  • You are only fetching one row from the result set: $row = mysqli_fetch_assoc($result); Commented Dec 14, 2016 at 12:08
  • your query looks backwards to me Commented Dec 14, 2016 at 12:09

2 Answers 2

1

You have to iterate through results :

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

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

    // print stuff
}
Sign up to request clarification or add additional context in comments.

Comments

1

mysqli_fetch_assoc is returning a result set.

You have to loop through this result set and handle each result separately.

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
}

1 Comment

Actually mysqli_fetch_assoc doesn't return a result set. But rather "the fetched row in the result set".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.