<?php

                            $sql    =" SELECT * FROM `comments` ";
                            $result = mysqli_query($db, $sql);

                            while($row = mysqli_fetch_assoc($result)){
                            $comment_id              = $row['comment_id'];
                            $comment_company_id      = $row['comment_company_id'];
                            $comment_name            = $row['comment_name'];
                            $comment_content         = $row['comment_content'];
                            $comment_email           = $row['comment_email'];
                            $comment_status          = $row['comment_status'];
                            $comment_date            = $row['comment_date'];

                echo "<tr>";
                echo "<td>$comment_id   </td>";
                echo "<td>$comment_name </td>";
                echo "<td>$comment_content</td>";
                echo "<td>$comment_email</td>";
                echo "<td>$comment_status  </td>";

                                $sql           =" SELECT * FROM `waste_collection_companies_info_table` WHERE `company_id`='$comment_company_id'";
                                $result        = mysqli_query($db, $sql);

                                while($row    = mysqli_fetch_assoc($result)){
                                $company_id   = $row['company_id'];
                                $company_name = $row['company_name'];

                                echo "<td><a href ='../post.php?p_id=$company_id'>$company_name</a></td>";
                                }

                echo "<td>$comment_date </td>";

                echo "<td><a href='companies.php?source=edit_companies&p_id='>approve</a></td>";
                echo "<td><a href='companies.php?delete='>disapprove</a></td>";
                echo "<td><a href='companies.php?delete='>delete</a></td>";

                echo "</tr>";
                }

                ?> </tbody> </table> <?php
$sql    =" SELECT * FROM `comments` ";
$result = mysqli_query($db, $sql);
while($row = mysqli_fetch_assoc($result))
{
    $sql           =" SELECT * FROM `waste_collection_companies_info_table` WHERE `company_id`='$comment_company_id'";
    $result        = mysqli_query($db, $sql);
    while($row    = mysqli_fetch_assoc($result))
    {
    }
}

The above is a summary of your code. As you can see you are reusing your $result in both while loops. When the inner while is done, the outer while will no longer be valid. Solution: use different variables for your inner and outer while.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.