PDO: how to populate html table with rows from database table?

Try this. I cleaned up your snippet. You don’t need $sql->setFetchMode(PDO::FETCH_ASSOC);, I’m not sure what you are trying to do, but that’s not how you are able to fetch arrays.

Furthermore, if($sql->rowCount() != 0) { is a noobie way of checking to see if there are records. If rowCount doesn’t equal to 0, display records? That doesn’t sound right. Either way, you can reduce this line to just if($sql->rowCount()) { because rowCount and num_rows already returns a 1 or a 0. 1 meaning true and 0 meaning false. When you throw rowCount and num_rows into an if statement while having rowCount and num_rows in the first if statement, if there are records, rowCount and num_rows will display the records accordingly and if there isn’t, it’ll display whatever is in the else statement. So there really is no need for !=0 or ==1 or <1 or >0 when rowCount and num_rows already return what is needed. When you do !=0 or ==1 or <1 or >0 to rowCount and num_rows, you are being redundant.

<?php
require('includes/config.php'); // CONNECTION to Database

$sql = $dbh->prepare("SELECT id, name, age FROM info WHERE name = :name");
$sql->execute([':name' => $name1]);

if($sql->rowCount()) {
?>
<table border="0">
    <tr COLSPAN=2 BGCOLOR="#6D8FFF">
        <td>ID</td>
        <td>Name</td>
        <td>Age</td>
    </tr>
<?php
while($row = $sql->fetch(PDO::FETCH_ASSOC)) {
?>
    <tr>
        <td><?php print($row['id']); ?></td>
        <td><?php print($row['name']); ?></td>
        <td><?php print($row['age']); ?></td>
    </tr>
<?php
}
?>
</table>
<?php
} else {

    print('There are no records at this moment.');

}

I usually get this a lot when I have typos in my .htaccess file. 500 usually relates to your Apache or IIS configuration file, 404 usually means the file doesn’t exist, 403 usually means you don’t have folder or file permissions, 401 usually means the username or password is wrong when trying to access an authentication protected folder either with Apache or IIS, and many other neat and fun errors that usually are done through configuration files.