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.

Edited by pritaeas: Fixed question. Please do not use "leet speak", but try to post in proper English.

4 Years
Discussion Span
Last Post by bradly.spicer

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

Edited by bradly.spicer

Votes + Comments
Nice AND MySQLi !
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.