Skip to content

Instantly share code, notes, and snippets.

@Ajax30
Last active July 16, 2018 15:39
Show Gist options
  • Save Ajax30/d26199983a6f63aed2cae47e7f7b75bc to your computer and use it in GitHub Desktop.
Save Ajax30/d26199983a6f63aed2cae47e7f7b75bc to your computer and use it in GitHub Desktop.
Use PDO fetch method to display table rows
<?php
$sql = 'SELECT * FROM people';
$result = $db->query($sql);
$numrows = $result->rowCount();
?>
<table>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Gender</th>
</tr>
<?php if ($numrows > 0) {
echo "<p>We have found $numrows results:</p>";
while($row = $result -> fetch(PDO::FETCH_ASSOC)) { ?>
<tr>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['last_name']; ?></td>
<td><?php echo $row['gender']; ?></td>
</tr>
<?php }
} else {
echo "<p>There are no results to display</p>";
}?>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment