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.

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 :)


                $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 */

            /* close connection */
commented: Nice AND MySQLi ! +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.