Create HTML table from sql table

Create HTML table from sql table

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

Stefan Bogdanescu

Founder & Senior Architect

Rendering SQL Data as HTML Tables in PHP

Transforming raw database rows into a structured HTML table requires more than just looping through results; it demands a shift in how you handle data structures and security. Your current approach uses mysql_fetch_array to iterate over records, but simply echoing the Name field breaks the flow required for a table. To display the last 10 results across fields like ID, Name, Email, University, Languages, and Experience, you need to construct HTML tags that wrap each row and cell dynamically based on the data array.

The immediate priority when working with legacy PHP code is addressing the database extension you are using. The mysql_* functions shown in your snippet belong to an extension deprecated since PHP 5.5 and removed entirely in PHP 7.0 due to critical security flaws, specifically the lack of prepared statements support which makes SQL injection prevention nearly impossible. If you are still relying on this code, migrating away from mysql_connect is not optional; it is a hard requirement for any production environment. The modern standard is either PDO or MySQLi, both of which enforce secure query execution patterns.

To fetch the last 10 results efficiently, you should modify your SQL query to include an offset and limit clause rather than fetching all rows in memory and slicing them in PHP. This improves performance by reducing network overhead between the database server and your application. With PDO, the implementation looks like this:

try {
    $pdo = new PDO("mysql:host=localhost;dbname=apploymentdevs", "root", "root");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // Fetch only the last 10 rows ordered by ID descending
    $stmt = $pdo->query("SELECT * FROM demo ORDER BY ID DESC LIMIT 10");
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    die("Database connection failed: " . $e->getMessage());
}

Once you have the data in a PHP array, generating the HTML table involves iterating through that specific dataset. You must ensure the output is wrapped in <table>, <thead>, and <tbody> tags to maintain semantic correctness. Each row requires a <tr> tag, and every column within that row needs a <td> tag.

Here is how you can structure the loop to render all required fields:

echo "<table border='1' cellpadding='5'>";
foreach ($results as $row) {
    echo "<tr>";
    echo "<td>" . htmlspecialchars($row['ID']) . "</td>";
    echo "<td>" . htmlspecialchars($row['Name']) . "</td>";
    echo "<td>" . htmlspecialchars($row['Email']) . "</td>";
    echo "<td>" . htmlspecialchars($row['University']) . "</td>";
    echo "<td>" . htmlspecialchars($row['Languages']) . "</td>";
    echo "<td>" . htmlspecialchars($row['Experience']) . "</td>";
    echo "</tr>";
}
echo "</table>";

A critical detail often missed by beginners is the use of htmlspecialchars(). Since you are pulling data from a database, user inputs like names or university names might contain special characters such as <, >, or ". Without sanitization, these characters could break your HTML structure or trigger Cross-Site Scripting (XSS) vulnerabilities. This function ensures that all output is safe to render in a browser context.

If you are building larger applications where database connectivity logic needs to be reusable and scalable, consider adopting the Laravel framework. Laravel provides robust tools for handling database queries through Eloquent ORM and simplifies generating HTML views. For instance, you can use paginate() to handle large datasets gracefully or pass data directly to Blade templates using @foreach loops, which abstract away the manual HTML tag concatenation entirely. This approach scales significantly better as your application grows beyond simple static pages.

Our network