Php showing only one database value

0

The php is showing only 1 value in the tables, I needed it to show all, one underneath the other.

<?php require 'pages/header.php'; ?>    
<?php
    if(empty($_SESSION['cLogin'])) {
        ?>
        <script type="text/javascript">window.location.href="login.php";</script>   
        <?php
        exit;
}
?> 
<div class="container">
    <h1>Controle de Clientes | Agro Grama</h1>
    <a href="add-clientes.php" class="btn btn-success">Adicionar Clientes</a>
    <table class="table table-striper">
        <thead>
            <tr>
                <th>Nome</th>
                <th>E-mail</th>
                <th>Telefone</tb>
                <th>Endereço</th>
                <th>Cidade</tb>
                <th>Firma</th>
                <th>CNPJ</th>
                <th>CPF</th>
            </tr>
        </thead>
        <tr>
            <td>
                <?php
                    $sql = $pdo->prepare("SELECT nome AS nome FROM clientes");
                    $sql->execute();

                    $ln = $sql->fetchObject();

                    echo ($ln->nome); 

                    ?></th>

            </td>
            <td>
                <?php
                    $sql = $pdo->prepare("SELECT email AS email FROM clientes");
                    $sql->execute();

                    $ln = $sql->fetchObject();

                    echo ($ln->email); 

                    ?>
            </td>
            <td>
                <?php
                    $sql = $pdo->prepare("SELECT telefone AS telefone FROM clientes");
                    $sql->execute();

                    $ln = $sql->fetchObject();

                    echo ($ln->telefone); 

                    ?>
            </td>
            <td>
                <?php
                    $sql = $pdo->prepare("SELECT endereco AS endereco FROM clientes");
                    $sql->execute();

                    $ln = $sql->fetchObject();

                    echo ($ln->endereco); 

                    ?>
            </td> 
            <td>
                <?php
                    $sql = $pdo->prepare("SELECT cidade AS cidade FROM clientes");
                    $sql->execute();

                    $ln = $sql->fetchObject();

                    echo ($ln->cidade); 

                    ?>
            </td>
            <td>
                <?php
                    $sql = $pdo->prepare("SELECT firma AS firma FROM clientes");
                    $sql->execute();

                    $ln = $sql->fetchObject();

                    echo ($ln->firma); 

                    ?>
            </td>
            <td>
                <?php
                    $sql = $pdo->prepare("SELECT cnpj AS cnpj FROM clientes");
                    $sql->execute();

                    $ln = $sql->fetchObject();

                    echo ($ln->cnpj); 

                    ?>
            <td>
                <?php
                    $sql = $pdo->prepare("SELECT cpf AS cpf FROM clientes");
                    $sql->execute();

                    $ln = $sql->fetchObject();

                    echo ($ln->cpf); 

                    ?>
            </td>

        </tr>
        <tr>
        </tr>   
    </table>
</div>
<?php require 'pages/footer.php'; ?>
    
asked by anonymous 23.10.2017 / 00:50

1 answer

1

In your example instead of making a query for each field you want to display, you must change the query to return all fields and iterate over the returned records:

<?php
$sql = $pdo->prepare("SELECT nome, email, telefone, endereco, cidade, firma, cnpj FROM clientes");
$sql->execute();
foreach ($sql->fetchAll() as $row) {
    ?>
    <tr>
        <td><?php echo $row['nome']; ?></td>
        <td><?php echo $row['email']; ?></td>
        <td><?php echo $row['telefone']; ?></td>
        <td><?php echo $row['endereco']; ?></td>
        <td><?php echo $row['cidade']; ?></td>
        <td><?php echo $row['firma']; ?></td>
        <td><?php echo $row['cnpj']; ?></td>
    </tr>
    <?php
}
?>
    
23.10.2017 / 01:12