Last active
July 16, 2018 15:39
-
-
Save Ajax30/d26199983a6f63aed2cae47e7f7b75bc to your computer and use it in GitHub Desktop.
Use PDO fetch method to display table rows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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