Limit a loop

0

I'm practicing PHP recently and I know how to loop with While, but I wanted to limit this loop to not break a column of the site.

<?php

        include_once 'conexao.php';

        $sql = "select * from perfil";

        $result = mysqli_query($con, $sql);

        while ($row = mysqli_fetch_array($result)){

    ?>
                <article class="novos-titulos">
<h3><a href="perfil.php?idperfil=<?php echo $row["idperfil"];?>"><?php echo mb_strimwidth($row["titulo"], 0, 20, "..." ); ?></a></h3>

                    <a href="perfil.php?idperfil=<?php echo $row["idperfil"];?>"><img src="img/<?php echo $row["capa"]; ?>" alt=""></a>
                    <span>Total: <?php echo $row["episodios"]; ?></span>

                </article>
  <?php } mysqli_close($con);?>
            </div>
    
asked by anonymous 23.07.2017 / 19:25

1 answer

0

An efficient solution to limit the number of iterations in the loop is to limit the return of the SQL query (this prevents loading of data that will not be used):

select * from perfil limit 5;
    
23.07.2017 / 21:46