Show values from a MySQL database table inside a HTML table on a webpage

Show values from a MySQL database table inside a HTML table on a webpage

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

Stefan Bogdanescu

Founder & Senior Architect

Rendering MySQL Data into HTML Tables

Converting raw database records into a readable HTML table is a fundamental web development task, yet it often trips up beginners because the terminology can be misleading. You aren't just "displaying" data; you are bridging two distinct worlds: the relational structure of your SQL database and the document model of your web page.

To achieve this, you need to perform three sequential steps: query the database, pass the results to your view layer, and iterate over those results to generate valid HTML tags. The specific example provided involves a table named tickets with fields like submission_id, formID, IP, name, email, and message.

Querying the Data Source

The first step is always fetching the data from MySQL. Using raw SQL directly in your view file is generally discouraged because it couples your presentation layer tightly to your database logic. Instead, you should use an ORM (Object-Relational Mapper) or a query builder provided by your framework.

In a PHP environment using the PDO extension (which powers Laravel), you would typically fetch all rows from the tickets table like this:

$stmt = $pdo->prepare("SELECT submission_id, formID, IP, name, email, message FROM tickets");
$stmt->execute();
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);

This returns an array of associative arrays. Each element in $records represents a single row from your database, where keys correspond to column names (e.g., submission_id, name). This structure is essential because it allows you to access data by key rather than by index, making your code more readable and less prone to errors if you add or remove columns later.

Mapping Data to HTML Structure

Once you have the array of records in your PHP script, you need to pass them to your Blade template (or plain HTML file). In Laravel, this is handled via a controller returning a view. The critical part happens inside the view where you iterate over the data using a foreach loop.

You must ensure that every row from the database generates a <tr> element, and every column generates a <td> or <th> element. A common mistake developers make is trying to output all fields in a single cell or forgetting to close tags, which breaks the table layout entirely.

Here is how you would render the specific HTML structure requested:

<table border="1">
  <thead>
    <tr>
      <th>Submission ID</th>
      <th>Form ID</th>
      <th>IP</th>
      <th>Name</th>
      <th>E-mail</th>
      <th>Message</th>
    </tr>
  </thead>
  <tbody>
    @foreach ($records as $record)
      <tr>
        <td>{{ $record['submission_id'] }}</td>
        <td>{{ $record['formID'] }}</td>
        <td>{{ $record['IP'] }}</td>
        <td>{{ $record['name'] }}</td>
        <td>{{ $record['email'] }}</td>
        <td>{{ $record['message'] }}</td>
      </tr>
    @endforeach
  </tbody>
</table>

Handling Data Safety and Escaping

A critical technical detail often overlooked in simple examples is data escaping. The output example shows text like "John Johnny" or an email address. If a user submits the message field containing <script>alert('XSS')</script>, displaying it directly without escaping will execute that script in your users' browsers.

When using Blade templating syntax (as shown above), curly braces {{ }} automatically escape HTML entities by default. This means any HTML tags inside the database fields are rendered as plain text, neutralizing cross-site scripting attacks. If you were using a raw PHP file instead of Blade, you would need to manually run htmlspecialchars($record['message'], ENT_QUOTES, 'UTF-8') before echoing the value.

Future Considerations: The Ticket Number

The source material mentions a planned field called ticket_number. While not strictly necessary for rendering the current table, it is worth noting how your code adapts when the schema changes. If you add this column to your MySQL table and alter your view logic, you simply need to update your query to include ticket_number and add a corresponding <td> or <th> in your HTML loop.

Because the iteration logic (foreach) is abstracted from the specific column names, adding new fields is non-disruptive. You are not hardcoding "123456789"; you are dynamically pulling whatever value exists at that index in your array. This flexibility is why using database abstractions like PDO or Eloquent is superior to raw SQL concatenation for dynamic table rendering.

For more robust data handling and model management, checking out resources on LaravelCompany can provide deeper insights into how modern frameworks handle database interactions seamlessly.

Our network