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.
shelexelex 0 Light Poster
Edited by pritaeas because: Fixed question. Please do not use "leet speak", but try to post in proper English.
Dani AI
Generated
Good start, — the usual pattern is correct: run a SELECT and loop the result set to print one table row per record. ’s reply shows that idea in practice. Below is a cleaner, safer pattern you can apply immediately: use a parameterised DB API, set the connection charset, and always HTML‑escape values before printing them.
<?php
$dsn = 'mysql:host=localhost;dbname=your_db;charset=utf8mb4';
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];
$pdo = new PDO($dsn, 'dbuser', 'dbpass', $options);
$stmt = $pdo->query('SELECT id, name, email, created_at FROM my_table');
echo '<table class="results">';
echo '<thead><tr><th>Id</th><th>Name</th><th>Email</th><th>Created</th></tr></thead>';
echo '<tbody>';
$found = false;
while ($row = $stmt->fetch()) {
$found = true;
echo '<tr>';
echo '<td>' . htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8') . '</td>';
echo '<td>' . htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8') . '</td>';
echo '<td>' . htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8') . '</td>';
echo '<td>' . htmlspecialchars($row['created_at'], ENT_QUOTES, 'UTF-8') . '</td>';
echo '</tr>';
}
if (! $found) {
echo '<tr><td colspan="4">No records found.</td></tr>';
}
echo '</tbody></table>'; Troubleshooting tips: enable exceptions or log SQL errors while developing, verify the connection and table name, and view the page source if the table seems to render oddly. For security, never print raw DB values — always escape them to prevent XSS. For large tables, add LIMIT/OFFSET and implement pagination or server‑side processing instead of fetching everything at once. Separating the DB logic (prepare/execute/fetch) from HTML output will make the code easier to maintain and style.
bradly.spicer 47 Posting Whiz in Training
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();
?>
Edited by bradly.spicer
pritaeas commented: Nice AND MySQLi ! +14
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.