1,105,281 Community Members

Create html table and populate with records from databse in php

Member Avatar
shelexelex
Light Poster
35 posts since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm a newbie to this. Hi, i've been trying to display all database records on an html table i still cant think anything out. I only know how to display a predefined number of rows but i need it to display all db records. I know it has to do with some loops but i dont know how to go about it. I wil really appreciate any help to this. Thanks.

Member Avatar
bradly.spicer
Posting Whiz in Training
238 posts since Oct 2012
Reputation Points: 47 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 6 [?]
 
1
 

Hey Shel,

it's quite simple really :)

So lets say in your database you have 3 columns. customer_name,postcode and mainarea.

What the code below does is create a table with headers (hence the echo <Th>) and then it uses fetch_object() to create objects of each header and display them!

All you have to do then, is style the table :)

            <?PHP

                $tbl_name = '';
                $query = "SELECT * FROM $tbl_name";

                if ($result = $mysqli->query($query)) {

                /* fetch object array */
                echo '<table align="center">';
                echo" <th>Name</th><th>Postcode</th><th>Mainarea</th><th>Expiration date</th>";
                while ($obj = $result->fetch_object()) {

                    echo "<tr><td>".$obj->customer_name."</td><td>".$obj->postcode."</td><td>".$obj->mainarea."</td><td>".$obj->expiration."</td></tr>";
                }
                 echo "</table>";
                /* free result set */
                $result->close();
            }

            /* close connection */
            $mysqli->close();
            ?>
Question Answered as of 6 Months Ago by bradly.spicer
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article