How to display data from a database into a html table?

Is there a way to take data from a database, like phpmyadmin, and display it into a html table?
Thank you

Yes.

Is there a followup question?

2 Likes

ok thanks, how is this done?
Thank you

just output the data as a html table. about 114.000.000 results for a three-word search. have you tried anything so theres a real problem?

This is a crude, basic example. I don’t necessarily do it exactly like this, as it can be a bit more complex in most cases and should maybe have a bit of error handling. But keeping it simple, first the php to run the query:-

<?php
    // First connect to the database via your connection insert file
    include_once $_SERVER["DOCUMENT_ROOT"]."/includes/pdo-conn.php"; 
    
    // This is a prepared statement, not necessary with this simple query with no variables, but anyway...
    $sql = $conn->prepare("SELECT This, That, Other FROM MyTable") ; 
    
    // Execute the query, if there were variables, they could be bound within the brackets
    $sql->execute() ;
?>

Then the html to display the table:-

<table>
    <thead>
        <tr>
            <th>This</th>
            <th>That</th>
            <th>The Other</th>
        </tr>
    </thead>
    <tbody>
        <!--Use a while loop to make a table row for every DB row-->
        <?php while( $row = $sql->fetch()) : ?>
        <tr>
            <!--Each table column is echoed in to a td cell-->
            <td><?php echo $row['This']; ?></td>
            <td><?php echo $row['That']; ?></td>
            <td><?php echo $row['Other']; ?></td>
        </tr>
        <?php endwhile ?>
    </tbody>
</table>
4 Likes

I would

  1. Run PHPMyAdmin
  2. Use Google Web tools to “View Source”
  3. Copy the HTML
  4. Paste my clipboard into a new file
  5. Add code to connect to your database (@SamA74 has provided a great start for that)

There was a time - when the Internet was very young - long before Sitepoint and StackOverflow - even before the existence of Google. During that time I learned HTML by using “View Source” in the browser (Netscape Navigator, by the way). There was no one to ask; in the true spirit of hacking it was a matter of deconstructing things that exist and learning how they are built.
That spirit seems to be lost in an age of INSTANT answers.

5 Likes

Fortunately, now there is. :wink:

Of course it doesn’t mean spoonfeeding, the best way to learn is getting your hands dirty.
So alx please try to implement the good advice given so far, and don’t hesitate to come back with more questions if you get stuck

4 Likes

Make sure you have scanned php doc at php.net about mysql.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.