Fetch and display data from database in table format in HTML form using PHP

Fetch and display data from database in table format in HTML form using PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Fixing Row Spans in PHP Database Table Displays

When rendering data from a database into an HTML table, it is common to encounter issues where multiple rows of data appear concatenated within a single <tr> element. In your specific scenario, the data fetch logic using mysql_query and mysql_fetch_array is functioning correctly; however, the HTML structure responsible for displaying that data is flawed. The issue lies in the loop iteration logic inside the table body.

Currently, your code places all fetched rows into a single <tr>. The browser interprets this as one row containing multiple cells, where each cell might contain multiple values depending on how you echo them. To achieve the desired output where every database record occupies its own visual row in the HTML grid, you must wrap the content generation for each record in a new <tr> tag inside your while loop.

Correcting the Loop Structure

The core fix involves moving the opening <tr> tag before the while loop begins and closing it after the loop finishes, or—more commonly and cleanly—opening and closing the row tags around every individual iteration. Here is how the corrected PHP section should look:

<div class="box-body table-responsive no-padding">
    <table class="table table-hover">
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Designation</th>
            </tr>
        </thead>
        <tbody>
            <?php 
            if (mysql_num_rows($result) > 0) {
                while ($row = mysql_fetch_array($result)) {
                    echo "<tr>";
                    echo "<td>" . $row['cand_number'] . "</td>";
                    echo "<td>" . $row['cand_fname'] . "</td>";
                    echo "<td>" . $row['cand_desc'] . "</td>";
                    echo "</tr>";
                }
            } 
            ?>
        </tbody>
    </table>
</div>

By placing echo "<tr>"; inside the loop, you ensure that the browser renders a distinct row for every record returned by the query. This separation is critical because HTML tables are strictly hierarchical; a <td> (table data) cell must always belong to a parent <tr>. Without the closing </tr> tag at the end of each iteration, all subsequent data merges into the previous line until the loop ends.

Security and Deprecation Warnings

While fixing the display logic is the immediate priority, it is essential to address the underlying code used for database interaction. The mysql_* functions you are using (mysql_query, mysql_fetch_array) have been deprecated since PHP 5.5 and completely removed in PHP 7.0. Continuing to use these functions will result in fatal errors if your server environment has been updated or if you attempt to install a newer version of PHP.

For modern applications, you should migrate to either PDO or MySQLi. If you are building on top of the Laravel framework, the built-in Eloquent ORM provides a robust abstraction layer that handles these interactions securely without writing raw SQL strings like SELECT candidate.cand_number.... Using prepared statements is mandatory to prevent SQL injection vulnerabilities, which are inherent risks when concatenating user input or dynamic conditions directly into query strings.

If you are maintaining legacy codebases where immediate migration isn't possible, ensure your server remains on PHP 7.4 or below and consider updating your code to use PDO as a transitional step. Resources like https://laravelcompany.com often provide guidance on upgrading legacy PHP projects to modern standards while preserving existing functionality.

Conclusion

The visual misalignment in your table stems from nesting multiple cells within a single row tag. Separating each database record into its own <tr> element resolves the layout issue immediately. As you refactor this code, prioritize updating the database connection layer to support current PHP versions and implement prepared statements to ensure the security and longevity of your application.

Our network